WordPress.org

Forums

Customizr
[resolved] How may display a custom list of posts on a page? (21 posts)

  1. acub
    Member
    Posted 1 year ago #

    Is there a function to display a custom list of posts on a page, using the same layout used on latest articles page?

    For example, I'd like to list my latest posts on both front page and another page, using the theme's list of posts layout.

    How would I go about that?

  2. ElectricFeet
    Member
    Posted 1 year ago #

    Would this work? http://wordpress.org/plugins/posts-in-page/ (I've not tried it)

  3. acub
    Member
    Posted 1 year ago #

    It does, but it doesn't show them exactly as the theme does. The circle images are not displayed at all and comment links are not styled as in the theme. I guess I have to build a custom template file for it. Was hoping this won't be necessary.

    Thank you for your time.

  4. acub
    Member
    Posted 1 year ago #

    @Nikeo: Is there some documentation regarding the use of template files/theme's functions/params in order to display a custom list of posts making use of the awesome theme layout (alternating image bubbles, display if icons in front of post titles, number of comments bubble, etc.) ?

    Do I need to change/declare some globals before the loop is run or during it?

    Thank you.

    In case I haven't made my scope clear, I want to display the latest posts on first page exactly as they are shown in the posts page. Eventually, I will probably keep only a featured list of posts on first page, but getting a custom list of posts from wordpress is simple. All I would like to know now is how to make use of the theme template/layout.

  5. acub
    Member
    Posted 1 year ago #

    Not very elegant, but it does the trick:
    Version 1:
    In functions.php of your childtheme:

    add_shortcode('my-featured-articles', 'my_featured_articles');
    function my_featured_articles() {
    	global $wp_query;
    	$temp_query = $wp_query;
    	$args = array (
    	'numberposts' => 10);
    // feel free to input whatever params you want in the $args array,
    // see WP_Query class documentation for that
    	$wp_query = new WP_query($args);
    	ob_start();
    		do_action('__loop');
    		$output = ob_get_contents();
    		ob_end_clean();
    	$wp_query = $temp_query;
    	return '</article></div>'.$output.'<div><article>';
    }

    In your page just write [my-featured-articles] and make sure it's not contained in any html tag from that page (div, span, table) or it will generate layout problems.

    Version 2:
    Alternatively, you could create a template file starting from index.php from customizr folder, copying it to your child theme folder, adding

    global $wp_query;
    $temp_query = $wp_query;
    $args = array (
    'numberposts' => 10);
    // feel free to input whatever params you want in the $args array,
    // see WP_Query class documentation for that
    $wp_query = new WP_query($args);

    before the line do_action('__loop'); and

    $wp_query = $temp_query;

    right after that line. Save it with a custom name (my-custom-layout.php) in your child theme folder, upload and select it as the page template from the page atributes in edit page, dashboard. If you do it this way, you no longer need to edit functions.php of your child theme and it will no longer matter what you write in the page content, as it will never be displayed. Instead, the list of posts will show, nicely formatted according to theme layout.

    Cheers.

  6. stickFinger
    Member
    Posted 1 year ago #

    Hey acub

    Thank you very much for sharing your code
    This was one of my questions HERE

    I´m trying with the template approach and it´s working very nice.

    BTW>IMHO this is the very only one feature that i miss on customizr.
    The devs might consider using that for a future update, don´t you think so?

    Once more, THANKS

  7. cobolatre
    Member
    Posted 1 year ago #

    This is fantastic.
    But I have a problem: With "Version 1" and "version 2" list of posts on a page, but the second page it loads the same 10 latest articles.
    You know why?

    http://pedernal.org/blog/

  8. stickFinger
    Member
    Posted 1 year ago #

    I guess that we need to enter the correct parameter on the $args = array but with my poor php I don´t know what to do...

    Anyone? acub help...:-)

  9. stickFinger
    Member
    Posted 1 year ago #

    @acub any light on this, please :-)

    @cobolatre did you find a solution for this?

  10. acub
    Member
    Posted 1 year ago #

    It's a new WP_Query. Parameters here. You could pass a query string or an args array.

  11. cobolatre
    Member
    Posted 1 year ago #

    @ stickFinger no! all dark, but I'm on it, thanks

    @ acab Yes, ok. I think I understand this

    There are 30 post. The blog have 3 pages with all post. This is ok.
    Page 1 have ten post, page 2 have the same post, page 3 have the same post.

    I do not know why

    http://pedernal.org/blog/
    http://pedernal.org/blog/page/2/
    http://pedernal.org/blog/page/3/

  12. acub
    Member
    Posted 1 year ago #

    You have to get the paged parameter from the initial query and pass it to the new query. Between

    global $wp_query;

    and

    $temp_query = $wp_query

    you have to insert:

    if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }

    and, of course, you need to put $paged in the query array:

    $args = array (
    'numberposts' => 10,
    'paged' => $paged);

    Haven't tested it, but it should do the trick.

  13. cobolatre
    Member
    Posted 1 year ago #

    jo jo jo jo! Yes, Yes, Yes!
    thank you very much, is perfect

    Version 2: "my-custom-layout.php" in your child theme

    <?php
    /**
     * Template Name: Blog.
     *
     *
     * @package Customizr
     * @since Customizr 1.0
     */
    get_header();
    
        do_action( '__fp_block' );
    
        do_action( '__breadcrumb' );
          ?>
            <div class="container" role="main">
    
                <div class="row">
    
                    <?php if ( !tc__f( '__is_home_empty')) : ?>
                        <?php
                            do_action( '__sidebar' , 'left' );
    
    global $wp_query;
    
    if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }
    
    $temp_query = $wp_query;
    $args = array (
    'numberposts' => 10,
    'paged' => $paged);
    
    // feel free to input whatever params you want in the $args array,
    // see WP_Query class documentation for that
    $wp_query = new WP_query($args);
    
    			        	    do_action( '__loop' );
    
    $wp_query = $temp_query;
    
                            do_action( '__sidebar' , 'right' );
                        ?>
                    <?php endif; ?>
    
                </div><!--#row -->
    
            </div><!-- #container -->
        <?php
    
    get_footer();
    ?>
  14. stickFinger
    Member
    Posted 1 year ago #

    @acub

    Thanks for sharing your code :-)

    @cobolatre
    Thanks for sharing your new template

    @theme developers

    Please consider adding a blog template like that on future updates...please :-)

  15. cobolatre
    Member
    Posted 1 year ago #

    Ups! Update Customizr 3.0.10 this solution fail!!!

  16. acub
    Member
    Posted 1 year ago #

    That's probably because the file structure has changed.

    What you need to do is take the new index.php from parent and make the template again. The principle is the same. I haven't tried 3.0.10 yet. If you get stuck I'll take a peek to get you going.

  17. SLCrane
    Member
    Posted 1 year ago #

    I'm using the latest version of wordpress and trying to implement the first solution but the do_action('loop') seems to be getting just that page's content rather than the posts. Anyone any idea what the problem could be?

  18. acub
    Member
    Posted 1 year ago #

    You either missed the initial

    global $wp_query;

    or the

    $wp_query = new WP_query($args);

    What are your args, btw?

  19. stickFinger
    Member
    Posted 1 year ago #

    @cobolatre

    Take a look, extracted from tehme´s site HERE:

    "
    Important note for Child Theme users

    The templates and the hook structure have been modified in this version. Check the template files (index.php, header.php, footer.php, comments.php) to adapt your child theme if needed."
    `

  20. acub
    Member
    Posted 1 year ago #

    I have made a new topic on this subject, as a how to. The code is simplified a bit and I also explain there how to make templates for custom lists of posts for Customizr.

    I'll mark this one as resolved as it contains code and solutions that are no more working in the current theme version.

  21. cobolatre
    Member
    Posted 1 year ago #

    OK. Thank you! I have not had time to work on this

Topic Closed

This topic has been closed to new replies.

About this Theme

About this Topic