Wordpress query_post with variable category - wordpress

I'm trying to put my blog content into bootstrap tabs, where each tab is a category so it is kind of a filter.
Everything is working smoothly but I can't separate dynamically the categories posts on the tabs.
Here is my first code, where I make the tabs getting the current categories: (all good here)
<ul id="catstab" class="nav nav-tabs nav-stacked cat_list">
<?php $categories = get_categories(); {
foreach($categories as $category){
echo "<li><a href='#" . $category->slug . "' data-toggle='tab'>" . $category->name . "</a></li>";
} // end foreach
}
?>
</ul>
Then I make the containers for each tab with a loop inside each one: (all good here too)
<div id="cattabscontent" class="tab-content">
<?php foreach (get_categories(array('hide_empty'=>false)) as $category)
{
echo '<div class="tab-pane fade" id="' . $category->slug . '">';
echo get_template_part("loop");
echo '</div>';
} ?>
</div>
Finally this is the query_post from my loop, and this is where I have the problem, what I need here is to give the category variable ($current_cat) the value of the current category, so it will get the correct posts on every tab.
<?php query_posts(array(
'category_name' => '$current_cat',
));
?>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
. . .
I have thought to give the variable the value of the ID of his parent, but I think it is not possible using only PHP, and I have no idea of how to make it using AJAX or something like that.
Any idea?

Don't use single quotes around variable names, use double quotes. Single quotes will be interpreted literally not expanded to the value of the variable, so echo '$var'; will give you $var exactly. Your code should either use double quotes or no quotes when setting the category_name argument, assuming that $current_cat is equal to the name you want to query on.
double quotes
'category_name' => "$current_cat",
no quotes
'category_name' => $current_cat,

Try to use category__in parameter like this:
<?php query_posts(array(
'category__in' => '$current_cat_id',
));
?>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
Parameter category__in display posts that have only this category and not children of that category. You will have to use category ID in this case.
Parameter you are using category_name on the other hand displays posts that have this category and any children of that category.

Related

Edit search results template in Wordpress

I have created a page template with a nice layout using the lovely custom fields plugin so my client can easily update the content.
I created a loop on that page template that displays the relevant information nicely;
Here is the loop I made:
<?php
$args = array( 'post_type' => 'cripps_staff', 'posts_per_page' => 300 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="col-md-3 spacetop">';
echo '<a href="'.get_permalink().'">';
echo get_post_meta($post->ID,'image',true);
echo '</a>';
echo '<h2 class="staffname">';
echo get_post_meta($post->ID,'staff_name',true);
echo '</h2>';
echo '<h2 class="staffrole">';
echo get_post_meta($post->ID,'staff_role',true);
echo '</h2>';
echo '<h2 class="staffnumber">';
echo get_post_meta($post->ID,'staff_telephone_number',true);
echo '</h2>';
echo '<h2 class="staffemail">';
echo get_post_meta($post->ID,'staff_email_address',true);
echo '</h2>';
echo '</div>';
endwhile;
?>
I created taxonomies so the staff members are split into categories.
I am then using a plugin called Taxonomies filter to create those dropdown options you will see. When you select an element in the dropdowns, Wordpress goes to/changes the page to a custom search results page I created. I want my search results to be displayed exactly like my loop on the People's template. Currently it just spits it out the title in a h1 tag.
Here is the code I got from the Twenty Fourteen theme:
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next post navigation.
CrippsTheme_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
How can I get the search results to look exactly like my Post loop?
I managed to resolve this completely with the help of Pieter Goosen, who provided me with an awesomely detailed response, see the full answer on Wordpress development forum:
https://wordpress.stackexchange.com/questions/143023/edit-wordpress-loop-taxonomies-filter

Get image of a post in Wordpress

I want to make a slide show in wordpress, I made a category for it and now I want to get images of that category for my slide show. Here is part of my code:
<div id="slide-show">
<ul>
<?php query_posts('cat=1'); while(have_posts()): the_post();?>
<li>
<a href="<?php the_permalink();?>">
<img src="<?php
$image=wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'thumbnail');
echo $image[0];?>" alt="<?php the_title();?>" /></a>
</li>
<?php endwhile; wp_reset_query();?>
</ul>
</div>
But it does not work. Can anybody help me please?
First of all, I'll repeat myself:
First of all, don't use query_posts.
Please, check:
When should you use WP_Query vs query_posts() vs get_posts()?
When to use WP_query(), query_posts() and pre_get_posts
In this case, I'd suggest building a function to create the desired output. You put it inside your theme functions.php file. Put it at the end of the file.
If there is any ?> as the last thing of a PHP file, remove it, it is not necessary and may break your site if there is any white space after it.
That said, our function will print exactly the Html you want and it is called like this in any theme template file (index.php, single.php, page.php, etc).
<?php get_gallery(4); ?>
The number 4 is the Category ID.
And this is the function, check the comments in the code:
function get_gallery( $id )
{
// Simple query
$posts = get_posts( array(
'category' => $id,
'post_status' => 'publish',
'post_type' => 'post',
) );
// Start building the Html code
$slide_show = '
<div id="slide-show">
<ul>';
// Iterate through the results
foreach ( $posts as $post )
{
// Assign value and test if it exists at the *same time*
if( $thumb = get_post_thumbnail_id( $post->ID ) )
{
$permalink = get_permalink( $post->ID );
$image = wp_get_attachment_image_src( $thumb );
// Build the Html elements
$slide_show .= '
<li>
<a href="' . $permalink . '">
<img src="'. $image[0] . '" alt="' . $post->post_title .'" />
</a>
</li>';
}
}
// Finish the Html
$slide_show .= '
</ul>
</div>
';
// Print the Html
echo $slide_show;
}
Result:

Display wordpress posts using get_posts - first post shows no date

Hi am using get_posts to grab all posts tagged as 'news' and display them on a given page. I am using the_date() to get the date but what is weird is that the first post shows no date, whereas all posts after this display the date fine. Also I have used this identical code to show posts tagged as 'blogs' on another page, yet they work fine.
Here is the page:
http://appshare.nsdesign7.net/news/
Also here is the other page the same code is used on but works fine:
http://appshare.nsdesign7.net/blog/
<? $pageTitle = wp_title('',false,'');
if ($pageTitle == " News") { ?>
<?php $appsharenewspage = array( 'numberposts' => 10, 'order'=> 'ASC', 'orderby'=> 'title', 'category' => 3 );
$postslist = get_posts( $appsharenewspage ); foreach ($postslist as $post) : setup_postdata($post); ?>
<article class="newsstyle">
<span class="imagestyle"><?php the_post_thumbnail(array(120,120)); ?> </span>
<h3><?php the_title(); ?></h3>
<span class="date"><?php the_date(); ?></span>
<?php the_excerpt(); ?>
<div class="clear"></div>
</article>
<?php endforeach; ?>
<?php } ?>
Try using:
echo get_the_date();
Read Special Note on: http://codex.wordpress.org/Template_Tags/the_date
the_date() is restricted to only show the date once per day. So if you have more then one post from the same day, it will only show on one of those posts.
Use the_time instead.
Try using <?php echo get_the_date(); ?> instead.
the_date displays date if the post is published on the same day only.
Reference: http://codex.wordpress.org/Function_Reference/get_the_date
The date only occurs once per day. ie for 2 posts on the same day one will be without the date. It is a WP function. To get what you want replace with the php function get time. try to use the_time() instead of the_date()

next_post_link() function give same URL of post in wordpress

When I am writing below code for recent posts and then after I write code for next and previous link then that function will give me same link of post. If I am comment out "$tags = wp_get_post_tags($post->ID);" This line then It'll print next post link. How can I resolved this error ? please help me.
<?php
//for use in the loop, list 5 post titles related to first tag on current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo '<div class="articlecontent font16 bold fontgray">Related Posts</div>';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>5,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<div class="articlecontent"><ul>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo "</ul></div>";
}
}
?>
<div class="nextprevbar">
<div class="prevtopic"><?php
previous_post_link( '%link', '<img border="0" alt="" src="'.get_template_directory_uri().'/images/prev-bullet.gif">' . _x( ' ', 'Previous post link', 'twentyten' ) . ' %title' ); ?></div>
<div class="nexttopic"><?php next_post_link( '%link', '%title <img border="0" alt="" src="'.get_template_directory_uri().'/images/next-bullet.gif">' . _x( ' ', 'Next post link', 'twentyten' ) . '</span>' ); ?></div>
</div>
The problem has to be in the $post variable. Before this line is executed:
while ($my_query->have_posts()) : $my_query->the_post(); ?>
the $post var holds the current post data (I guess this code is inside single.php?). But after this line, and inside the loop, the $post var holds your various recent posts, one by one (you setup the $post variable when you call the_post() ).
After that loop (bellow the endwhile), $post will hold the data of the last post retrieved in that loop.
previous_post_link() and next_post_link() need to access $post for reference of the current post, but they are taking as reference the last post of your recent posts, instead of the post being read by your user.
I don't know what the html structure of this page is, but I would put the recent posts list AFTER the navigation links (next, previous posts). That would solve the problem if I am right, and in my opinion it would be semantically clearer.
Or you could also try this:
Add this line:
$currentPost = clone $post;
Before:
$my_query = new WP_Query($args);
And add this line:
<?php $post = $currentPost; ?>
Before you call next and previous post links functions.

Display Link to Category in Wordpress?

I am trying to figure out how to display the category of an article, and a link to the category. Any help would be greatly appreciated.
If you want to do this on post page you can add something like the following to your single.php file of your theme.
<div class="meta">Posted in: <span><?php the_category(', ') ?> </span></div>
Here's some info that will be of use:
http://codex.wordpress.org/Template_Tags/wp_list_categories
Basically you can call: <?php wp_list_categories( $args ); ?> and this will output what you're looking for.
Thr $args parameter is an array of settings strings that lets you change the order, style, depth etc, on links returned.
Note that: <?php the_category(', ') ?>will display the category as a link. which is good.... but if you want only the category URL (that is, the category link only), then you will have to use the <?php get_category_link($category_ID); ?> the $category_ID is required. once you fix that in, the category URL will be returned.
Consider the example:
<?php
// Get the ID of a given category
$category_id = get_cat_ID( 'Category Name' );
// Get the URL of this category
$category_link = get_category_link( $category_id );
?>
<!-- Print a link to this category -->
Category Name
Now you can see how we got the category ID and then using it to get the category Link. Hope this answers your question well enough?
You can use get_the_category()
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= ''.$category->cat_name.''.$separator;
}
echo trim($output, $separator);
}
?>

Resources