Wordpress and Webdesign Forum go41 » WordPress Code Snippets

  1. thanks for your recent help on the bottom nav bar, I haven't had a chance to implement yet (or say thank-you!) but it looks sensible enough.

    Now - a quick one - how can I change the front page so that the 10 posts (not featured/Headline) remain the same? I'm sure I've seen you solve this one before so apologies!

    Posted 1 year ago #
  2. I would use 'sticky posts' for that. In post editor under the 'Publish' headline you find 'Visibility: Public'
    here you can edit a post to 'Stick this post to the front page'

    Try this with any post you want to show on frontpage, they should line up there on top of other posts.

    The crucial part: I guess they will line up (or appear) in Headlines and Featured too! You do not want this so you have to edit index.php in the queries for Headline and Featured.

    To exclude sticky posts being included at the beginning of Headlines and Featured you have to tell wordpress to return posts in the natural order. The parameter you have to add is: "&caller_get_posts=1"

    The headlines query looks now something like this:

    <?php query_posts("showposts=1&category_name=Headline"); ?>

    modify to:

    <?php query_posts("showposts=1&caller_get_posts=1&category_name=Headline"); ?>

    Featured with modification:

    <?php query_posts("showposts=4&caller_get_posts=1&category_name=Featured"); $i = 1; ?>

    Tell me if that works, I use it in http://themes.go41.de/?wptheme=aranovo2

    Another way would be to set any page as 'static frontpage', but you have to create a complete new frontpage etc...

    Posted 1 year ago #
  3. Thanks f-t-cat, very sage advice and I've stickied 10 posts that I'd like on our front page and it works a treat - funnily enough they don't crash the featured/headline post because I think it must run them by date.

    HOWEVER

    What it has done is increased the number of posts on the front page to 20 as it's trying to show the latest content as well. Ideally what I'd like is it just to have

    HEADLINE FEATURED

    CATEGORY BAR

    10 STICKY POSTS

    NEXT PAGE

    Do you know how I'd stop it from displaying the normal posts as well on the front page - http://www.iamstaggered.com if you'd like to see what I mean.

    Thanks for your help!

    Posted 1 year ago #
  4. Didn't think think about this! Unfortunately WordPress adds Stickies to the total number of posts per page.
    Try to count the stickies you have and built the difference to the posts per page.

    your loop for the lower part under div id="front-list" in Arthemia looks something like this:

    <?php
          $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
          query_posts("cat=-27,-28&paged=$page&posts_per_page=5"); ?>

    Modify to:

    <?php
    $num = intval(get_option('posts_per_page')) - intval(count(get_option('sticky_posts')));
    $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("cat=-27,-28&paged=$page&posts_per_page=$num"); ?>

    this will get your setting of posts per page (I guess it's 10?) and will take off the number of the additional stickies. $num will be the variable used in posts_per_page=$num in the last line of this code

    I do not know what happens if you have more stickies than posts per page, try to stay with ten and ten to display your stickies and zero normal posts.

    What happens with pagination? If it's not working anymore we should change with a conditional code for "if(!is_paged())" (first page)

    BACKUP index.php FIRST!!

    Posted 1 year ago #
  5. Thanks for that - I've copied over the new code in place of the old bit but it's still calling the additional 10 posts onto the front page as well as the 10 stickies. Am I missing something?

    Posted 1 year ago #
  6. did you also change in the last line: posts_per_page=$num ?

    try to echo $num and look for a number on top of your frontlist

    <?php
    $num = intval(get_option('posts_per_page')) - intval(count(get_option('sticky_posts')));
    $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("cat=-27,-28&paged=$page&posts_per_page=$num"); ?>
    <?php echo $num; ?>

    it might work without intval in that line

    $num = get_option('posts_per_page') - count(get_option('sticky_posts'));

    or try this:

    <?php
    $num = count(get_option('sticky_posts'));
    $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts('cat=-27,-28&paged=$page&posts_per_page='. ( 10 - $num )); ?>

    hope no typing error, didn't try

    Posted 1 year ago #
  7. hi Joern - do you mean that I should add an actual number in place of $num? I've tried those configurations you suggested and so far it's not working.

    BTW I'm changing this in the index.php in themes>arthemia>index.php - is that right?

    Posted 1 year ago #
  8. in the last code I gave I suggested to use 10 - $num, this subtracts the $num from 10, $num is the amount of sticky posts you have.
    I do not know what happens if this gets 0 (zero) or a negative number in case you have 10 or more stickies..
    it's in index.php under the div id="front-list", right, you might have excluded other category numbers than cat=-27,-28 - these are the IDs of Featured and Headlines normally

    I see you echo a 0 (zero) on top of your frontlist, reduce sticky posts by one or set posts per page to one more, if it echoes a '1' it should show your stickies plus one latest.

    Posted 1 year ago #
  9. I love you Joern. This code


    <?php
    $num = count(get_option('sticky_posts'));
    $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts('cat=-27,-28&paged=$page&posts_per_page='. ( 10 - $num )); ?>

    And reducing stickies to 9 worked a treat. Many thanks to you and Mr or Mrs Cat.

    Posted 1 year ago #
  10. No.. second page shows first page again, with the same content..
    check how it works by disabling the plugin wp_pagenavi, if previous and next page work without the plugin.
    It might help to switch the plugin off and on again to solve this problem.
    echo the $num again to see the value on second page etc

    Just looked into the code of some of my themes, I used another query just to cheat pagination.
    In your index.php you find close to the bottom:

    <?php endwhile; ?>
    
    	<div class="navigation">
    		<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
    ....

    add after endwhile and before div class="navigation" this:

    .....endwhile...
    
    <?php if(!is_paged()) { //to make pagination work if there are less posts than reading settings
      $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	query_posts("paged=$page&posts_per_page=(get_option('posts_per_page')");
    } ?>
    
    ....div class="navigation".....

    and try...

    Posted 1 year ago #
  11. Oh bum! Good job you checked!

    I've tried adding that code but it's still giving me the same as the front page.

    I'm afraid I didn't quite understand the instruction re: pagenavi.

    Any other thoughts?

    Posted 1 year ago #
  12. I suggested to disable pagenavi plugin in 'Plugins' just to see if normal Wordpress pagination works. Arthemia switches to next(previous)_posts_link if the plugin is not used. If pagination works without the plugin we know that this old plugin is the problem.

    Other thoughts? Start all over again and query on frontpage for Stickies only and on following pages as you did before with excluding sticky posts being included at the beginning of posts after the first page.
    roughly:

    if not paged "if(!is_paged())"
    -> query for sticky posts
    if paged (else)
    ->run your old query with one more parameter "caller_get_posts=1" to not show stickies on top

    Just have to copy and paste some code together ;-)

    Posted 1 year ago #
  13. Haha, like that it's "just" all you have to do :)

    I deactivated PAGE-NAVI and it still kept the stickies on page 2, so I'm at a loss!

    Posted 1 year ago #
  14. try this as replacement for that queries starting with $num

    <?php
    	$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    		if( is_front_page()) { //if is first page not paged
    			$args = array(
    			'posts_per_page' => 10,
    			'post__in'  => get_option('sticky_posts'),
    			'caller_get_posts' => 1
    			);
    			query_posts($args); // only ten stickies
    		} else { // not frontpage
    			query_posts("cat=-27,-28&caller_get_posts=1&paged=$page&posts_per_page=10"); // old normal query, set your excluded categories if you have
    		}
    ?>

    should show max. 10 stickies on frontpage
    should show any normal stuff from page two
    if pagnavi doesn't work - try without

    Posted 1 year ago #
  15. Ahhh! Tried your last solution and now it doesn't want to show any other pages! I think I might have to admit defeat on this one.

    Posted 1 year ago #

RSS feed for this topic

Reply »

You must log in to post.

Join us! or log in (lost password?):