• I have a site that I’ve been working on. The homepage is a template from Themler. I’m trying to get the homepage to only show the 4 most recent posts and the blog page show whatever I choose to set in the Reading section of settings.

    Here’s the code I found for the blog_4.php it’s pulling it from. I did find the is_home section and I’m assuming it needs to go in there.

    start code ______________________

    <?php
    function theme_blog_4() {
    global $post;
    $need_reset_query = false;
    if (is_page()) {
    $page_id = get_queried_object_id();
    if (theme_get_meta_option($page_id, ‘theme_show_categories’)) {
    $need_reset_query = true;
    query_posts(
    wp_parse_args(
    ‘category_name=’ . theme_get_meta_option($page_id, ‘theme_categories’),
    array(
    ‘paged’ => get_query_var(‘paged’, get_query_var(‘page’, 1))
    )
    )
    );
    }
    }

    if (!$need_reset_query && theme_is_preview()) {
    global $theme_current_template_info;
    if (isset($theme_current_template_info)) {
    $template_name = $theme_current_template_info[‘name’];
    $ids = theme_get_option(‘theme_template_’ . $template_name . ‘_query_ids’);
    if ($ids) {
    $need_reset_query = true;
    $ids = explode(‘,’, $ids);

    query_posts(array(
    ‘post_type’ => ‘any’,
    ‘post__in’ => $ids,
    ‘paged’ => get_query_var(‘paged’, get_query_var(‘page’, 1)),
    ));
    }
    }
    }
    ?>
    <div class=” bd-blog-4″>

    <?php
    if ( is_home() && ‘page’ == get_option(‘show_on_front’) && get_option(‘page_for_posts’) ){
    $blog_page_id = (int)get_option(‘page_for_posts’);
    $title = ‘‘ . get_the_title($blog_page_id) . ‘‘;
    echo ‘<h2 class=” bd-container-23 bd-tagstyles bd-custom-blockquotes bd-bootstrap-btn bd-btn-warning”>’ . $title . ‘</h2>’;
    }
    ?>

    <?php
    if (have_posts()) { ?>
    <div class=” bd-grid-7″>
    <div class=”container-fluid”>
    <div class=”separated-grid row”>
    <?php while (have_posts()) {
    the_post();

    $id = theme_get_post_id();
    $class = theme_get_post_class();
    ?>

    <div class=”separated-item-24 col-md-6 “>

    <div class=”bd-griditem-24″>
    <article id=”<?php echo $id; ?>” class=” bd-article-5 clearfix <?php echo $class; ?>”>
    <?php
    if (!is_page() || theme_get_meta_option($post->ID, ‘theme_show_page_title’)) {
    $title = get_the_title();
    if(!is_singular()) {
    $title = sprintf(‘%s‘, get_permalink($post->ID), strip_tags($title), $title);;
    }
    if (!theme_is_empty_html($title)) {
    ?>
    <h2 class=” bd-postheader-5″>
    <div class=”bd-container-inner”>
    <?php echo $title; ?>
    </div>
    </h2>
    <?php
    }
    }
    ?>

    <?php echo theme_get_post_thumbnail(array(‘imageClass’ => ‘ bd-imagestyles-17’, ‘class’ => ‘ bd-postimage-4’)); ?>

    <?php
    if (theme_is_preview() && is_singular()) {
    $editor_attrs = ‘data-editable-id=”post-‘ . theme_get_the_ID() . ‘”‘;
    } else {
    $editor_attrs = ”;
    }
    ?>
    <div class=” bd-postcontent-9 bd-tagstyles bd-custom-blockquotes bd-bootstrap-btn bd-btn-warning <?php if (theme_is_preview()) echo ‘ bd-post-id-‘ . theme_get_the_ID(); ?>”>
    <div class=”bd-container-inner” <?php echo $editor_attrs; ?>>
    <?php echo(is_singular() ? theme_get_content() : theme_get_excerpt()); ?>
    </div>
    </div>
    </article>
    <?php
    global $withcomments;
    if (is_singular() || $withcomments){ ?>
    <?php
    if (theme_get_option(‘theme_allow_comments’)) {
    comments_template(‘/comments_4.php’);
    }
    ?>
    <?php } ?>
    </div>
    </div>
    <?php
    }
    ?>
    </div>
    </div>
    </div>
    <?php
    } else {
    ?>
    <div class=”bd-container-inner”><?php theme_404_content(); ?></div>
    <?php
    }
    ?>

    </div>
    <?php
    if($need_reset_query){
    wp_reset_query();
    }
    }

    ______________ end code

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hello George.

    Where your query_posts currently reads:

    query_posts(array(
    'post_type' => 'any',
    'post__in' => $ids,
    'paged' => get_query_var('paged', get_query_var('page', 1)),
    ));

    …try inserting posts_per_page like so:

    query_posts( array(
    'post_type' => 'any',
    'post__in' => $ids,
    'posts_per_page' => 4,
    'paged' => get_query_var('paged', get_query_var('page', 1)),
    ));

    (You may need to alter a different query_posts() in your template, but you get the general idea here. ??

    It may be worth noting that there are several ways to Query a post, and there is a useful article in the Codex on the subject of query_posts.

    Thread Starter georgewaterman

    (@georgewaterman)

    Yeah, I’m noexpert at this and nothing is working for me. I basically just want the blog previews on the homepage to just show for as long as it’s on the homepage and the blog page show as many as I set in the settings of WP.

    It’s just not working for me and it keeps giving me a 500 everytime I try anything. I’m a Noob as scripting.

    The site it https://ruslanmusic.com/ if anyone cares to take a look.

    Moderator bcworkz

    (@bcworkz)

    beaudaignault is on the right track, except the query_posts() do not affect home page content. An easy oversight because the way you posted the code is very difficult to follow. For future reference, to post that much code please use pastebin.com and select the appropriate syntax highlighting when publishing your paste. Just provide the link here – much cleaner ?? And for short snippets here always enclose your code in `backticks`, or use the ‘code’ button.

    The home page content is managed by the default query. You could insert beaudaignault’s complete, revised query_posts() call just after the if ( is_home() && 'page' == get_option//... line. Using query_posts() is not very efficient, but it’s easy and it works.

    The problem you’re going to have though is the next page will still start on the default ‘posts_per_page’+1, not on post #5 as you would want. To overcome this you need specify an adjusted offset for every page. When you do this, the default pagination is broken so you end up completely managing all of your own pagination.

    A seemingly simple modification turns out to be quite an endeavor ??

    Thread Starter georgewaterman

    (@georgewaterman)

    Yeah, he doesn’t really care about starting over on 1 when getting to the blog page. He just wants to make sure that we can specify ?number of posts for the blog page and just the latest 4 on the homepage.

    Not sure i exactly get what you want me to put in but I can give it a shot.

    Thank you both for your time and efforts.

    Thread Starter georgewaterman

    (@georgewaterman)

    tried this which didn’t do anything at all but at least nothing exploded:

    if ( is_home() && 'page' == get_option('show_on_front') && get_option('page_for_posts') <strong>&& get_option('posts_per_page', 4)</strong>){
            $blog_page_id = (int)get_option('page_for_posts');
            $title = '<a href="' . get_permalink($blog_page_id) . '" rel="bookmark" title="' . strip_tags(get_the_title($blog_page_id)) . '">' . get_the_title($blog_page_id) . '</a>';
        echo '<h2 class=" bd-container-23 bd-tagstyles bd-custom-blockquotes bd-bootstrap-btn bd-btn-warning">' . $title . '</h2>';
    Thread Starter georgewaterman

    (@georgewaterman)

    ignore the html please. It’s not actually in the source.

    Moderator bcworkz

    (@bcworkz)

    I was thinking something like this:

    if ( is_home() && 'page' == get_option('show_on_front') && get_option('page_for_posts') ){
      query_posts( array(
        'post_type' => 'any',
        'post__in' => $ids,
        'posts_per_page' => 4,
        'paged' => get_query_var('paged', get_query_var('page', 1)),
      ));
      $blog_page_id = (int)get_option('page_for_posts');

    Never mind about the pagination, I missed the fact there is a separate blog page on my initial read, and even worse, that the fact is implied in the if statement itself.

    Thread Starter georgewaterman

    (@georgewaterman)

    nothing is working so far….

    Moderator bcworkz

    (@bcworkz)

    Please change is_home() to is_front_page() in my previous code.

    is_home() is only true when the blog index is the home page. Front page applies to either blog index or static front pages.

    The array item 'post__in' => $ids, might be an issue. It’s a theme option I’m unfamiliar with. If the above change doesn’t help try commenting this array item out.

    On my installation, these changes resulted in behavior like you wanted, as I understand it.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘maximum number of post on homepage’ is closed to new replies.