[Resolved] Show posts in my categories in random order

Home Forums Support [Resolved] Show posts in my categories in random order

Home Forums Support Show posts in my categories in random order

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #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

    #120385
    Tom
    Lead Developer
    Lead Developer

    Actually, 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 🙂

    #120431
    Eduardo 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,

    #120541
    Tom
    Lead Developer
    Lead Developer

    Hmm, just tested it and it’s working for me.

    Can you take a screenshot of your functions.php file for me?

    #122582
    Eduardo

    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,

    #122679
    Tom
    Lead Developer
    Lead Developer

    Hmm, any way you can take a screenshot of the page with the function on it as well? The code itself looks correct.

    #122712
    Eduardo

    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,

    #122782
    Tom
    Lead Developer
    Lead Developer

    Hmm, 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?

    #122886
    Eduardo 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,

    #122964
    Tom
    Lead Developer
    Lead Developer

    Hmm, 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?

    #122983
    Eduardo 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,

    #123199
    Tom
    Lead Developer
    Lead Developer

    Hmm, 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.

    #123442
    Eduardo 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,

    #123658
    Tom
    Lead Developer
    Lead Developer

    Hmm, I wonder if adding a higher priority would do the trick: https://gist.github.com/generatepress/bcee6fc33a22e578885c

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.