• Resolved CliveO

    (@cliveo)


    I found this on the forum

    <?php
    $howmany = 10;
    
    $pages = wp_list_pages("echo=0&title_li=");
    
    $pages_arr = explode("\n", $pages);
    
    for($i=0;$i<$howmany;$i++){
    	echo $pages_arr[$i];
    }
    
    ?>

    Which displays 10 pages in one of the widget area but I need it to show the latest 10 pages. Not sure which 10 it currently shows (I think it is alphanumeric) but it’s not the last 10. Anyone know how to hack this to show the latest 10 pages published and possible a way of excluding certain pages (comma separated possibly)

    [Moderator Note: Please post code or markup snippets between backticks or use the code button. As it stands, your code may now have been permanently damaged/corrupted by the forum’s parser.]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Do you actually mean the last 10 Posts? If so, where would you be displaying this list?

    Thread Starter CliveO

    (@cliveo)

    I know there is a widget to display posts and you can set how many to display. What I need is a sidebar widget that will display the 10 latest pages published. I have a site and there are 2 pages published every day, one in the morning and one in the afternoon.
    The reason it is pages is long and complicated so this is what I have.
    I just need to display 10 page links for the last 10 pages, 5 morning pages and 5 afternoon pages. I tried this:

    <?php
    $args = array(
    	'post_type' => 'page',
    	'orderby' => 'post_date',
    	'showposts' => '3'
            );
    $posts = get_posts($args);
    if ($posts) {
    foreach($posts as $post) {
    setup_postdata($post);
    ?>
    <p><a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    <?php
    }

    but just got the same link repeated. At least this code…

    <?php
    $howmany = 10;
    
    $pages = wp_list_pages("echo=0&title_li=");
    
    $pages_arr = explode("\n", $pages);
    
    for($i=0;$i<$howmany;$i++){
    	echo $pages_arr[$i];
    }
    
    ?>

    did show different links even if they were alphanumeric. I just need them to be in date order, showing only the latest 10.

    [Moderator Note: Please post code or markup snippets between backticks or use the code button. As it stands, your code has been permanently damaged/corrupted by the forum’s parser.]

    there is no harm looking into the Codex when using some functions;

    for instance the info for 'wp_list_pages()' will reveal that there is a 'sort_column' parameter:

    https://codex.www.ads-software.com/Function_Reference/wp_list_pages
    https://codex.www.ads-software.com/Function_Reference/wp_list_pages#Parameters

    sort_column
    (string) Sorts the list of Pages in a number of different ways. The default setting is sort alphabetically by Page title.

    • ‘post_title’ – Sort Pages alphabetically (by title) – default
    • ‘post_date’ – Sort by creation time.
    • ‘post_modified’ – Sort by time last modified.
    Thread Starter CliveO

    (@cliveo)

    I had a quick look, it doesn’t make a whole lot of sense to me if I’m honest. Just not sure how I add a ‘post_date’ sort column to this code.

    <?php
    $howmany = 10;
    
    $pages = wp_list_pages("echo=0&title_li=");
    
    $pages_arr = explode("\n", $pages);
    
    for($i=0;$i<$howmany;$i++){
    	echo $pages_arr[$i];
    }
    
    ?>

    $howmany=10; is pretty straighforward and I think $pages is a variable of the listed pages…then at (“echo=0&title I just get lost! Any ideas how I sort the pages by date and just show the latest 10?

    try:

    $pages = wp_list_pages("echo=0&title_li=&sort_column=post_date&sort_order=DESC");
    Thread Starter CliveO

    (@cliveo)

    You beauty! Thank you so much. How hard it was to do this? I have scoured the web for this solution. So here it is the final code that lists the latest 10 pages in a sidebar text widget.

    <ul>
    
    <?php
    $howmany = 10;
    
    $pages = wp_list_pages("echo=0&title_li=&sort_column=post_date&sort_order=DESC");
    
    $pages_arr = explode("\n", $pages);
    
    for($i=0;$i<$howmany;$i++){
    	echo $pages_arr[$i];
    }
    
    ?>
    
    <ul>

    Simply install the “PHP text widget” here https://www.ads-software.com/extend/plugins/php-text-widget/ and drop the snippet of code in a regular text sidebar widget and you are done other than styling it up with CSS.

    Thanks for the code. I’ve been trying to do this for a week now. Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Widget to display most recent 10 pages published’ is closed to new replies.