I have the following function in functions.php page
function viewpost($num)
{
echo $num;
query_posts('order=dsc&cat=$num & showposts=2');
while (have_posts()) : the_post();
?> <span> <?php the_title(); ?>
<?Php
echo get_the_post_thumbnail();
the_excerpt();
?>
<?Php
endwhile;
wp_reset_query();
}
When I call viewpost function for values of viewpost(1)(to view post from category one ) it shows correct values, but when I put the same function again viewpost(2) (to view post from category 2) it shows the previous function values i.e. from category. What can I do to get the post from different categories by changing the passing value
Without trying your code, I think the most likely problem is you're using single quotes. Variable names won't get expanded to their values. See this answer.
Try
query_posts("order=dsc&cat=$num&showposts=2");
instead of
query_posts('order=dsc&cat=$num & showposts=2');
This might be worth a read too. Using query_posts is normally not recommended.
Related
I have a normal posts page in an Anchor CMS theme. Beside the posts list is a category list, which I am intending to use as sorting filters for the blog posts. The category list has been output as follows, based on the Anchor Docs:
<?php foreach(Category::dropdown() as $id => $category): ?>
<div class="filter" data-filter=".category-<?php echo $id; ?>"><?php echo $category; ?></div>
<?php endforeach; ?>
I then have to include a corresponding class in each article or post within that list, which would look like this:
<?php if(has_posts()): ?>
<?php $i = 0; while(posts()): ?>
<article class="mix category-<?php echo category_id(); ?>">
<?php endwhile; ?>
<?php endif; ?>
Notice that the article class category-[num] corresponds with the data-filter on the div "filter". This is what allows sorting.
However any way I try to do this I am getting either doubled up posts or just not working. I had tried using a foreach statement as seen in the docs:
<?php foreach(Category::dropdown() as $id => $category): ?>
<article class="mix category-<?php echo category_id(); ?>">
<?php endforeach; ?>
but this makes posts double up, I assume because it is within a while loop?
In the database, categories and posts are in two separate tables, however the category ID is included in the posts table. I have looked for a way to echo this e.g. article_category_id but with no success so far.
How can I include the category ID in the posts list?
Okay, found the answer to my own question.
This was a simple solution actually, once I thought about it. The various function references in Anchor CMS such as article_category etc can be defined by the user by accessing anchor > functions > articles.php.
Within this file, and based on existing functions such as
function article_category() {
if($category = Registry::prop('article', 'category')) {
$categories = Registry::get('all_categories');
return $categories[$category]->title;
}
}
I created a new function for the category ID. It looks like this:
function article_category_id() {
if($category = Registry::prop('article', 'category')) {
$categories = Registry::get('all_categories');
return $categories[$category]->id;
}
}
and then echoed this out in the posts loops:
<?php echo article_category_id(); ?>
Simple as that!
is there a wordpress function that I can use to detect pages? example of what I want to do is below.
<?php if( is_FUNCTION_page('Contact Us') ) : ?>
...display this <div> / xhtml
<?php else: ?>
something else <div>
<?php endif;?>
Check is_page() function:
is_page();
// When any single Page is being displayed.
is_page(42);
// When Page 42 (ID) is being displayed.
is_page('Contact');
// When the Page with a post_title of "Contact" is being displayed.
is_page('about-me');
// When the Page with a post_name (slug) of "about-me" is being displayed.
is_page(array(42,'about-me','Contact'));
// Returns true when the Pages displayed is either post ID 42, or post_name "about-me", or post_title "Contact". Note: the array ability was added at Version 2.5.
Yeah. Use Wordpress is_page function to do that.
Example:
<?php if( is_page('Contact Us') ) : ?>
<div> Your If content goes here </div>
<?php else: ?>
<div> something else </div>
<?php endif;?>
NOTE:
Cannot Be Used Inside The Loop
Due to certain global variables being overwritten during The Loop is_page() will not work. In order to use it after The Loop you must call wp_reset_query() after The Loop.
I have this working query that successfully gets the custom field data in my page template file:
<?php $featuredpost_cat = get_field('featured_category_id'); ?>
If I echo that out into the page I get "23" the value of the custom field, so I know that is working, what I want to do is grab that value and use it as a query parameter.
Farther down my page I have this:
<?php query_posts( $featuredpost_cat . '&posts_per_page=1'); if (have_posts()) : while (have_posts()) : the_post(); ?>
All that this does is ignore my variable and return the latest post on the site.
I hope this is clear enough.
== Edit ===
In case I am not being clear, I want to get a custom field which is a category ID from the page, then use it in a query on the page template.
So I set the field as category ID: 23 and then call it in my query_posts function so that I only return posts from that category.
Maybe the full page of code will help: template code
How about
<?php query_posts( 'cat='.$featuredpost_cat . '&posts_per_page=1'); if (have_posts()) : while (have_posts()) : the_post(); ?>
I assume that $featuredpost_cat is a category id
Sorry, I don't understand your second code example. Are you trying to use ternary operator to accomplish this?
query_posts('cat='.$featuredpost_cat . '&posts_per_page=1');
if (have_posts()){
while (have_posts()){
the_post();
}
}
What does query_posts() and the_post()do? If query_post() fetches the posts, have_post() checks the existance of posts and the_post() echoes them on the page, the code above should work. If this is not the case, please tell what the functions do.
Edit. Removed the question mark.
I have a template file (trendingPosts.php) for showing 2 latest posts with the tag 'trending'. In the while loop for displaying these 2 posts, I take their ID's in an array so I can exclude them from the main wordpress loop later:
<div id="trendingWrap" class="clearfix">
<?php
$trending = new WP_Query();
$trending->query("showposts=2&tag=trending");
while($trending->have_posts()) : $trending->the_post();
$wp_query->in_the_loop = true;
$currentTrending[] = $post->ID;
?>
<div class="trendingStory">
<h2 class="trendingTitle"><?php the_title(); ?></h2>
</div><!-- end trendingStory -->
<?php endwhile; ?>
</div><!-- end trendingWrap -->
The problem is that I have an index.php in which I include the loop.php via get_template_part( 'loop', 'index' ); and I am unable to get the $currentTrending[] array that I made in trendingPosts.php. I need to get that array in my loop.php
Moreover, in my loop.php, I am excluding the 2 posts in the following way.
if(have_posts()): while(have_posts()) : the_post();
if( $post->ID == $currentTrending[0] || $post->ID == $currentTrending[1] ) continue;
Is this the right way to exclude posts? if anybody has a better way of doing this whole thing. Please let me know. Of course nothing works until I manage to get that array in loop.php so that is the main issue.
Thanks! I appreciate all the help.
You can easily create variables that you can access everywhere by using the $GLOBALS superglobal array.
Once set
$GLOBALS['mytheme_thisismyvar'] = 22;
You can then access it everwhere in the other templates:
$myvar = $GLOBALS['mytheme_thisismyvar'];
And use it where it suits. This works with sub-templates regardless how they get loaded.
Because the whole program shares this superglobal array, take care that you do not overwrite existing values.
Try moving your current trending code to the theme's functions.php, so that you can call on it whenever you need.
function getCurrentTrending() {
$trending = new WP_Query();
$trending->query("showposts=2&tag=trending");
while($trending->have_posts()) : $trending->the_post();
$wp_query->in_the_loop = true;
$currentTrending[] = $post->ID;
endwhile;
return $currentTrending;
}
You can then fetch that array from any template file:
$currentTrending = getCurrentTrending();
Hope that helps.
I am starting to write a custom theme for Wordpress. I have created a new page called 'Home' and set the front page to be a static page, selecting 'Home'. I want this page to show all posts with a category of 'news' plus a couple of images.
I then added front-page.php with the following:
<?php get_header(); ?>
<div class='detail'>
<?php if ( have_posts() ) {
query_posts( 'category_name=news' );
while ( have_posts() ) : the_post(); ?>
<h4><?php the_date('d/m/Y'); ?> - <?php the_title(); ?></h4>
<div class='post'><?php the_content(); ?></div>
<?php endwhile; }?>
</div>
<?php get_footer(); ?>
I've uploaded a couple of images and attached them to the 'Home' page. I now want to get the URL for the attached images. I've tried something like:
$images =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . get_the_ID());
but this only returns the ID of the most recent post being shown, even if I put the above code outside the loop. If I remove the static front page and navigate to the Home page then I get the correct results, any ideas how I can get the images for the Home page when I use it as a static page?
Forgive me if this is simple, first foray into PHP and wordpress development
I think your issue is that you're using get_the_ID(); outside of The Loop. the_ID(); and get_the_ID(); grab the current post ID from the loop; if used outside of The Loop, all you'll get is the last one. See: http://codex.wordpress.org/Function_Reference/get_the_ID
To get the ID of the current page, try:
$page=get_page_by_title($page_name);
$images =& get_children('post_type=attachment&post_mime_type=image&post_parent='.$page->ID);
If that doesn't work, there's a function at http://wordpress.org/support/topic/how-to-get-page-id-using-php?replies=5 (Where I found the above code) that does the same thing.
Hope this helps!