0

I have a code for a custom page i'm creating that I want to have my own blog archive (formatted customized)

this is my code:

<?php                                                     
                    $args = array(
                                    'posts_per_page' => '-1',
                                    'post_type' => 'post',
                                    'post_status' => 'publish',
                                    'category__in' => $quicksand_categories 
                                    );                     
                    $query = new WP_Query( $args );                                             
                    foreach ($query->posts as $item) {                      
                        $categories = wp_get_post_categories($item->ID);
                        ?>
                        <li id="item" class="item" data-id="id-<?php echo $item->ID ?>" data-type="<?php foreach ($categories as $c) { echo $c . ' ';}?>" >
                        <?php  if (get_option('featured') == 'yes') { ?>
                           <a href="<?php echo get_permalink($item->ID); ?>">                   
                        <?php  echo get_the_post_thumbnail($item->ID);  ?></a>
                        <?php } ?>
                           <br />
                        <?php if(get_option('titles') == 'yes') { ?>
                            <h2><a href="<?php echo get_permalink($item->ID); ?>">
                        <?php echo get_the_title($item->ID); ?>
                            </a></h2>

                <?php echo apply_filters('the_content', $item->post_content); ?>

                <h6 class="alt-h"><a href="<?php echo get_permalink($item->ID); ?>">READ MORE</a></h6>
                <hr/>
                            <?php } ?>
                        </li>                                          
                    <?php  }  ?>
             </ul>

it seems to be working properly, but it doesn't acknowledge the fact that the posts that it's pulling have a "read more" in them. I only want this page to pull the information before the read more of my blog posts, not the entire post.

I originally tried to just use "the_content" but that pulls the contents of the actual page itself not that of the posts. so that's why i'm using

<?php echo apply_filters('the_content', $item->post_content); ?>

I'm probably missing something silly, but any help would be appreciated.

1

You're actually doing this in a way that is far more complicated than necessary. The foreach is actually excessive. What you want to do is something more like this:

$args = array(
    'posts_per_page' => '-1',
    'post_type' => 'post',
    'post_status' => 'publish',
    'category__in' => $quicksand_categories 
);                     

$query = new WP_Query( $args );

if ($query -> have_posts() {
    while ($query -> have_posts() {
        $query -> the_post();
        // Do your display stuff here
    }
}

This puts the Post object in a global $post variable, so now you can easily use functions like the_title() or the_permalink() to print content without passing around an ID value to a bunch of echo calls.


For example:

where you have: <a href="<?php echo get_permalink($item->ID); ?>">

You can now have: <a href="<?php the_permalink(); ?>">

Or, instead of $item->post_content, you can just use the_excerpt();

Ultimately, the_excerpt() is what you want to use in this case. Here's a link to the codex entry. There are also some good examples there of how to customize it to your liking (like including a custom "read more" link).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.