- This topic has 13 replies, 3 voices, and was last updated 8 years, 2 months ago by
Tom.
-
AuthorPosts
-
July 12, 2015 at 2:17 am #120323
Eduardo Garcia
Good Morning,
I’m trying to change the way of displaying posts in my categories to do it at random determinedly, showing only one in order ASC.
I put the following code in archive.php, but does not work
<?php /* Start the Loop */ ?>
<?php
if (is_category(‘4’)) {
query_posts($query_string . ‘&orderby=title&order=ASC’);
} else {
query_posts($query_string . ‘&orderby=rand’);
}
?>
<?php while ( have_posts() ) : the_post(); ?>You know as I can make it work properly?
Thanks
July 12, 2015 at 10:31 am #120385Tom
Lead DeveloperLead DeveloperActually, you should just have to add this function: https://gist.github.com/generatepress/bcee6fc33a22e578885c
Adding PHP: http://generatepress.com/knowledgebase/adding-php-functions/
That should do it 🙂
July 12, 2015 at 12:35 pm #120431Eduardo Garcia
Hi Tom,
thank you very much for your answer, I added:
add_action( ‘pre_get_posts’, ‘generate_random_category_posts’ );
function generate_random_category_posts( $query ) {
if ( $query->is_category() && $query->is_main_query() ) {
$query->set( ‘orderby’, ‘rand’ );
}
}to my functions.php I file my child theme, but still not working properly because no random sample randomly posts.
You can see here for example: http://ofertassingles.com/vacaciones-singles/.
This week I will be out so I’m not going to answer, however if you can help I’d appreciate it.
Best regards,
July 12, 2015 at 11:39 pm #120541Tom
Lead DeveloperLead DeveloperHmm, just tested it and it’s working for me.
Can you take a screenshot of your functions.php file for me?
July 21, 2015 at 1:39 am #122582Eduardo
Hi Tom,
sorry for the delay in responding but I’ve been away this past week. I send you my child theme functions.php, to see what happens and why not work me.
<?php /** * Generate child theme functions and definitions * * @package Generate */ add_filter( 'wpcf7_form_elements', 'do_shortcode' ); add_action( 'pre_get_posts', 'generate_random_category_posts' ); function generate_random_category_posts( $query ) { if ( $query->is_category() && $query->is_main_query() ) { $query->set( 'orderby', 'rand' ); } }
regards,
July 21, 2015 at 9:56 am #122679Tom
Lead DeveloperLead DeveloperHmm, any way you can take a screenshot of the page with the function on it as well? The code itself looks correct.
July 21, 2015 at 10:48 am #122712Eduardo
Hi Tom,
This is the screenshot of the code, not if this is what you asked me.
https://www.dropbox.com/s/r4iu0frshy8dvxp/screen-tom.jpg?dl=0
If it is not what you request, please tell me that you need to solve the problem. It may be a plugin that is not compatible?
Now I do not work or pages or posts, and I use the categories for pages, through the plugin:
http://wpthemetutorial.com/plugins/post-tags-and-categories-for-pages/
In other templates I used previously operated correctly the following code:
<?php
if (is_category(‘4’)) {
query_posts($query_string . ‘&orderby=title&order=ASC’);
} else {
query_posts($query_string . ‘&orderby=rand’);
}
?>Best regards,
July 21, 2015 at 6:05 pm #122782Tom
Lead DeveloperLead DeveloperHmm, looks fine. I wonder why it’s working for me and not you.
How are you viewing the category? By going to yourwebsite.com/category/category-name?
July 22, 2015 at 9:40 am #122886Eduardo Garcia
Hi Tom,
You can see my category links below as an example:
http://ofertassingles.com/vacaciones-singles/
http://ofertassingles.com/agosto/
http://ofertassingles.com/noticias-singles/But not if it influences my ‘/category/’ mediation is hidden by category plugin no base, just as I have in GP Hooks the following code:
After entry title:
<?php
if (in_category( array(‘3’, ‘4’, ‘5’, ‘7’, ’10’, ’22’, ’23’, ’24’, ’27’, ’28’, ’29’, ’50’, ’51’, ’52’, ’53’, ’54’, ’55’, ’57’, ‘267’, ‘348’, ‘352’) )) {
if(get_field(‘fechas2’))
echo ‘‘ . get_field(‘fechas2’) . ‘‘;
if(get_field(‘duracion2’))
echo ‘‘ . get_field(‘duracion2’) . ‘‘;
} else {
}
?>After content:
<?php
if (in_category(‘9’)) {
} elseif (is_category( array(‘8′,’3’, ‘4’, ‘5’, ‘7’, ’10’, ’22’, ’23’, ’24’, ’27’, ’28’, ’29’, ’50’, ’51’, ’52’, ’53’, ’54’, ’55’, ’57’, ‘267’, ‘348’, ‘352’) )) {
echo the_field(‘texto’);
} elseif (is_category( array(‘121’, ‘355’,’354′) )) {
echo the_excerpt();
} else {
}
?>
<?php
if (in_category(‘9’)) {
echo (‘Viaje no Activo‘);
} elseif (in_category(‘359’)) {
echo (‘Ultimas Plazas‘);
} elseif (is_category( array(‘3’, ‘4’, ‘5’, ‘7’, ‘8’, ’10’, ’22’, ’23’, ’24’, ’27’, ’28’, ’29’, ’50’, ’51’, ’52’, ’53’, ’54’, ’55’, ’57’, ‘267’, ‘348’, ‘352’) )) {
if(get_field(‘verde2’))
echo ‘‘ . get_field(‘verde2’) . ‘‘;
if(get_field(‘rojo2’))
echo ‘‘ . get_field(‘rojo2’) . ‘‘;
if(get_field(‘naranja2’))
echo ‘‘ . get_field(‘naranja2’) . ‘‘;
if(get_field(‘azul2’))
echo ‘‘ . get_field(‘azul2’) . ‘‘;
} else {
}
?>On the other hand I leave my list of plugins in case one of them is the cause of this error: 99 Robots Show IDs, Advanced Custom Fields, Akismet, Better WordPress Minify, Configurable Tag Cloud, Formulario de Contacto 7, Contact Form DB, Cookie Ley Española, Google XML Sitemaps, GP Premium, List category posts, Page Excerpts for WordPress Three, Post Tags and Categories for Pages, Really Simple CAPTCHA, RSS Includes Pages, Shareaholic , Simple 301 Redirects, Single Post Template, Tabby Responsive Tabs, TablePress, UpdraftPlus – Backup/Restore, WordPress PopUp, WP-No-Tag-Base, WP-Optimize, WP No Category Base, WP Smush, WP Super Cache, Yoast SEO
best regards,
July 22, 2015 at 11:44 pm #122964Tom
Lead DeveloperLead DeveloperHmm, the issue here is definitely the fact you’re pulling pages into masonry and not posts.
How are you calling pages instead of posts? Using a custom page template/category template?
July 23, 2015 at 2:05 am #122983Eduardo Garcia
Hi Tom,
the call to the pages so that they are treated as posts are using the following plugin:
https://wordpress.org/plugins/post-tags-and-categories-for-pages/
however it does not work in the posts, since the category:
http://ofertassingles.com/noticias-singles/
This set not only by posts and randomly sample
I do not use any custom template of pages or category
best regards,
July 23, 2015 at 11:56 pm #123199Tom
Lead DeveloperLead DeveloperHmm, you may have to reach out to the developer of that plugin to see if they have any ideas on how to display them randomly. The
pre_get_posts
hook only works when displaying posts, so it would have to be something different/custom when using pages like this.July 25, 2015 at 4:12 am #123442Eduardo Garcia
Hi Tom,
This is the code that has the plugin makes the call to the posts, I believe it also uses pre_get_posts, please if you can check it to see if something can help me set …
<?php
/*
Plugin Name: Post Tags and Categories for Pages
Plugin URI: http://wpthemetutorial.com/plugins/post-tags-and-categories-for-pages/
Description: Simply adds the stock Categories and Post Tags to your Pages.
Version: 1.3
Author: curtismchale
Author URI: http://wpthemetutotial.com/about/
License: GNU General Public License v2.0
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/class PTCFP{
function __construct(){
add_action( ‘init’, array( $this, ‘taxonomies_for_pages’ ) );
/**
* Want to make sure that these query modifications don’t
* show on the admin side or we’re going to get pages and
* posts mixed in together when we click on a term
* in the admin
*
* @since 1.0
*/
if ( ! is_admin() ) {
add_action( ‘pre_get_posts’, array( $this, ‘category_archives’ ) );
add_action( ‘pre_get_posts’, array( $this, ‘tags_archives’ ) );
} // ! is_admin} // __construct
/**
* Registers the taxonomy terms for the post type
*
* @since 1.0
*
* @uses register_taxonomy_for_object_type
*/
function taxonomies_for_pages() {
register_taxonomy_for_object_type( ‘post_tag’, ‘page’ );
register_taxonomy_for_object_type( ‘category’, ‘page’ );
} // taxonomies_for_pages/**
* Includes the tags in archive pages
*
* Modifies the query object to include pages
* as well as posts in the items to be returned
* on archive pages
*
* @since 1.0
*/
function tags_archives( $wp_query ) {if ( $wp_query->get( ‘tag’ ) )
$wp_query->set( ‘post_type’, ‘any’ );} // tags_archives
/**
* Includes the categories in archive pages
*
* Modifies the query object to include pages
* as well as posts in the items to be returned
* on archive pages
*
* @since 1.0
*/
function category_archives( $wp_query ) {if ( $wp_query->get( ‘category_name’ ) || $wp_query->get( ‘cat’ ) )
$wp_query->set( ‘post_type’, ‘any’ );} // category_archives
} // PTCFP
$ptcfp = new PTCFP();
Best Regards,
July 25, 2015 at 11:52 pm #123658Tom
Lead DeveloperLead DeveloperHmm, I wonder if adding a higher priority would do the trick: https://gist.github.com/generatepress/bcee6fc33a22e578885c
-
AuthorPosts
- You must be logged in to reply to this topic.