Loop in Sidebar Done Incorrectly? - wordpress

I'm trying to add a "Movie Reviews" Section to the sidebar, but it's also altering my main loop. Heres the Code I have:
//Movie All Reviews
function display_movie_reviews( $query ) {
if ( $query->is_category('movies') ) {
$query->set( 'tag', 'movie-reviews' );
}
}
add_action( 'pre_get_posts', 'display_movie_reviews' );
It's displaying the reviews like I want, but the category page at site.com/movies/ is only displaying only the reviews
here is the loop for the sidebar:
<ul>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php add_action( 'pre_get_posts', 'display_movie_reviews' ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'sidebar-reviews', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
</ul>
and sidebar-reviews:
<li>
<a class="sidebar-thumb" href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'sidebar-thumb',array('title' => "")); ?></a>
<div class="sidebar-text">
<a class="sidebar-link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="sidebar-rating"><!-- rating here --></span>
</div>
</li>

Try wp_reset_query(); http://codex.wordpress.org/Function_Reference/wp_reset_query
This function destroys the previous query used on a custom Loop.
Function should be called after The Loop to ensure conditional tags
work as expected.

Related

How do I use get_content WordPress function?

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 -->

How to link a post to its category in WordPress

I want to add a link when my post gets click on. The link has to go to the category of the post. I've got this far but now I'm stuck. Can anyone show me how I can do this?
<?php
$args = array(
'category_name' => 'portriats',
'posts_per_page' => 1
);
$qry = new WP_Query($args);
if ( $qry->have_posts() ) :
while ( $qry->have_posts() ) : $qry->the_post();
$postcat = get_the_category( $post->ID );
?>
<div class="hometile">
<a href="<?php get_category_link( $postcat) ?>">
==> **I need to get the category of the post and then let PHP print the link of the categorey in the href**
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php
endwhile;
endif;
?>
It will display primary category of post with link
<?php $qry = new WP_Query($args ); ?>
<?php if ( $qry->have_posts() ) : ?>
<?php while ( $qry->have_posts() ) : $qry->the_post(); $postcat = get_the_category();?>
<div class="hometile">
<a href="<?php echo get_category_link( $postcat[0]->term_id ); ?>">
<?php echo $postcat[0]->name; ?>
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>

Instead of showing the latest posts, how to show the latest post from each category on the homepage in WordPress?

I have the code to display the latest posts on my website, but I wonder if there is a way to make a list of the latest posts, displaying only one post per category. Let's say I have 7 categories, so only 7 posts will be displayed on the page. What should I do?
<?php if ( ! is_single() ) { ?>
<div class="post-container">
<?php } ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
//Post Title code
//Post Thumbnail code
//Post Content / Excerpt code
//Post Meta code
</article> <!-- /post -->
<?php if ( ! is_single() ) { ?>
</div>
<?php
<?php } ?>
It's very easy to add latest post from each category.
First of all get all the categories of blog by using below code:
$categories = get_categories();
Then use foreach ( $categories as $category ) {} to tell WordPress to run through each of these categories in turn and run the code inside the braces.
Now you need to define the arguments for your query. Inside the braces, add this:
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
);
Next, insert your query, using the WP_Query class:
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
<?php the_excerpt( __( 'Continue Reading <span class="meta-nav">→</span>', 'twentyfourteen' ) ); ?>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
This will display each category posts in your home page. Please try to use it and let me know if you have any issue.

Displaying a specific post category in Wordpress

I am trying to display posts from a specific category on my website which currently displays all posts. Here is the code I have on the section of the page below:
<?php
// the query
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>18)); ?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Change your code inside the loop to something like below code and here cat = 3 determines the category 3.
<ul>
<?php
$catquery = new WP_Query( 'cat=3&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<li>
<h3>
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a>
</h3>
<?php the_content(); ?>
</li>
<?php endwhile; ?>
</ul>
pass the argument cat = 3 or category id for which you want to show post

How do I sort category results on this code?

I have this code on the category.php file and I need to sort the results by ASC cause right now results are in the reverse order.
Thanks in advance.
<ol class="search-results-list">
<?php
// Return Event Items
$i = 0;
while (have_posts()) : the_post();
if( get_post_type() == 'researcher' ) {
$i++; ?>
<li><strong><?php the_title(); ?></strong><br /> <?php print_excerpt(200); ?></li>
<?php }
endwhile;?>
<?php if( $i == 0 ) { ?><li><?php _e( 'No results were found.', 'qns' ); ?></li><?php } ?>
<!--END .search-results-list -->
</ol>
Add below code into your functions.php file
if ( !function_exists( 'get_cat_id_by_slug' ) ) {
function get_cat_id_by_slug ($get_slug) {
$CatId = get_term_by( 'slug', $get_slug, 'category' );
$CatId = $CatId->term_id;
return $CatId;
}
}
Add below code into your category.php
<?php
$category_id = get_cat_id_by_slug('researcher');
$args = 'cat=' . $category_id . 'order=asc';
query_posts($args);
if (have_posts()) :
while (have_posts()) : the_post();
?>
<ol class="search-results-list">
<li>
<strong>
<?php the_title(); ?>
</strong><br />
<?php print_excerpt(200); ?>
</li>
</ol>
<?php
endwhile;
else :
?>
<ol class="search-results-list">
<li><?php _e( 'No results were found.', 'qns' ); ?></li>
</ol>
<?php
endif;
?>

Resources