I have been trying to alter my current code, but it makes no sense to me (it doesn't do what I was told it does). All I want to do is only display posts with category 13 on my homepage and I want to be able to add multiple categories to a post.
Post 1 (Category 13 and 1) = displayed on homepage
Post 2 (Category 13 and 4 and 5) = displayed on homepage
Post 3 (Category 6 and 1) = not visible on homepage
Post 4 (Category 2) = not visible on homepage
This is my current code to only show category 13 on my homepage, if another category is added to the post it won't be displayed at all.
<?php get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php
if (is_home()) {
query_posts("cat=-6,-4,-1,-11");
}
?>
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'cat' => 13,
'posts_per_page' => 5,
'paged' => $paged);
query_posts($args);
?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Geen berichten beschikbaar', 'twentyeleven' ); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php _e( 'Helaas, er zijn nog geen gearchiveerde berichten in deze categorie. ', 'twentyeleven' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
You're looking way too hard at this code. You don't need to exclude anything, you just need to include cat 13, which will include everything that is in cat 13; even if it is in other categories as well. Just run a regular WP_Query() like so:
<?php get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php
if (is_home()) {
// The Query
$the_query = new WP_Query("cat=13, paged=".get_query_var('paged'));
}
?>
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Geen berichten beschikbaar', 'twentyeleven' ); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php _e( 'Helaas, er zijn nog geen gearchiveerde berichten in deze categorie. ', 'twentyeleven' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
(had to add a few charachters or I could not edit, sorry :P)
Try this...
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=6&cat=13'.'&paged='.$paged);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
Your stuff goes here...
<?php endwhile; ?>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
Try this code
<?php
global $post;
if (is_home()) {
$args = array( 'numberposts' => 5, 'category' => 13 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content( 'Continue reading <span class="meta-nav">»</span>); ?>
</div>
<?php endforeach;
}
?>
This will display the title and content. If you want additional information like date, author, category, tag etc, you may have to use other template tags. Also if you want to style these differently, then you have to use appropriate CSS.
Related
I am not able to fetch content while using the the_content WordPress function.
<!-- Start the Loop. -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( in_category( '3' ) ) : ?>
<div class="post-cat-three">
<?php else : ?>
<div class="post">
<?php endif; ?>
<!-- Display the Title as a link to the Post's permalink. -->
<h2><?php the_title(); ?></h2>
<!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
<small><?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?></small>
<!-- Display the Post's content in a div box. -->
<div class="entry">
<?php the_content(); ?>
</div>
<!-- Display a comma separated list of the Post's Categories. -->
<p class="postmetadata"><?php _e( 'Posted in' ); ?> <?php the_category( ', ' ); ?></p>
</div> <!-- closes the first div box -->
<!-- Stop The Loop (but note the "else:" - see next line). -->
<?php endwhile; else : ?>
<!-- The very first "if" tested to see if there were any Posts to -->
<!-- display. This "else" part tells what do if there weren't any. -->
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<!-- REALLY stop The Loop. -->
<?php endif; ?>
I have really tried but get empty description result kindly help
Here a example fetch description from post in Wordpress
<?php
$categories = get_the_category( $id );
if( $categories ){
// Assumes you just want the first category
print 'You’re in the ' . $categories[ 0 ]->name . ' category';
}
?>
<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$attachments = get_posts( array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null
) );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
?>
<li><?php echo wp_get_attachment_image( $attachment->ID, 'full' ); ?>
<p><?php echo apply_filters( 'the_title', $attachment->post_title ); ?></p>
</li>
<?php
}
}
endwhile; endif; ?>
</ul>
<p><?php echo apply_filters( 'the_title', $attachment->post_title ); ?></p> <!-- do you need change post_title for description -->
I have one custom loop in a custom homepage in WordPress that is pulling 4 posts with some banners in middle. All is working fine, however pagination always shows same posts. Is there a way for me to add pagination to this custom loop?
My index.php code:
<?php
query_posts(array(
'post_type' => 'post',
'showposts' => 4,
) );
?>
<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 3) : ?>
<!-- banners -->
<h1> <?php the_title(); ?> </h1>
<?php the_content(); ?>
<?php else : ?>
<h1> <?php the_title(); ?> </h1>
<?php the_content(); ?>
<!-- banners -->
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<p align="center"><?php posts_nav_link(); ?></p>
Inside your query_post array add this line:
'paged' => ( get_query_var('paged') ) ? get_query_var('paged') : 1,
and in settings -> reading set 4 posts per blog page.
I am a new of wordpress. I have a problem with pagination when I click to the next post it shows "not found". I installed the plugin wp pagenavi, and I put code in my blog post . It shows the pagination, but I have a problem with the link to the next post. Example when I click to the next post it is show
Something went Wrong!
404
-->
You can see at: http://westecmedia.com/events/
And this is my code in event-page.php:
<div id="content-events">
<div id="head-event"><h3>EVENTS</h3></div>
<div id="main-event">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="part-event">
<div id="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div id="event-dess">
<h2><?php the_title(); ?></h2>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content, 0, 300);
?>
</p>
<div id="read-more">Read More</div>
</div>
</div>
<div id="line-bottom"></div>
<?php endwhile; else: endif; ?>
<?php wp_pagenavi(); ?>
</div>
</div>
<?php get_footer(); ?>
Help me please ?
Maybe you missed the object here. You can try the code below:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'category_name'=>get_the_title(),
'post_status'=> array('publish', 'future')
);
query_posts($args);
instead:
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
I have this code:
<?php $q = new WP_Query(array(
'post_type' => 'oferty'
));
?>
<?php while ($q -> have_posts()) : $q -> the_post(); ?>
<!-- .post | id: <? echo $post->ID; ?> -->
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="entry">
<h1><?php the_title(); ?></h1>
<small><?php echo get_field('bank'); ?></small>
<?php the_content(); ?>
</div>
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<?php if($thumb) : ?>
<div class="bankimg" style="background-image: url('<?php echo $thumb[0];?>')"></div>
<?php endif; ?>
<div class="clear"></div>
</article>
<!-- /.post | id: <? echo $post->ID; ?> -->
<?php endwhile; wp_reset_query(); ?>
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
It uses wp_query to list all the posts from post type oferty. I set in WordPress options limit of posts to 2 and it works - but no pagination shows up. I tried WP PageNavi, WP Pagination and normal WordPress' prev/next linsk.
Take a look at the pagination parameters when querying for custom post types:
https://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
Specifically, try this:
$q = new WP_Query(array(
'post_type' => 'oferty',
'posts_per_page' => 2
));
I would like to have my page show the 2 most recent posts, so I'm using WP_Query and setting posts_per_page to 2, which works great, but it kills the pagination. Here is my code. How do I alter it to show two most recent posts and keep pagination?
<?php $wp_query = new WP_Query( array( 'posts_per_page' =>2 ));?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div id="ind_post">
<h2 class="post-title"><?php the_title(); ?></h2>
<div id="entry">
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<p><?php the_excerpt(); ?></p>
</div>
<h4 class="more-post">continue reading…</h4>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h2>No results found</h2>
<?php endif; ?>
<?php wp_reset_query(); ?>
This should do it
<?php $wp_query = new WP_Query( array( 'posts_per_page' =>2, 'paged=' . get_query_var( 'page' ) ));?>