I am trying to run a loop that queries only a specific category from my wordpress. this is what i have but it shows all of my post instead of just my post under the category of 'testimonials'
<div class="row">
<?php if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ('cat=$cat&showposts=5');
if ($posts) {
foreach ($posts as $post):
setup_postdata($post); ?>
<div class="col-sm-12">
<div class="box" data-toggle="modal" data-target="#myModal1">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
</div>
<?php endforeach;
}
}?>
</div>
Please any help would be great,
thnx
Thats because the arguments sent to get_posts isnt correct. Try:
$posts = get_posts(array(
'category'=>$cat,
'posts_per_page'=>5, //This changes how many posts are returned
'offset' => 0, //This changes where it search starts from
);
Have a look at the documentation here
There is also a better way to get the post category:
$cat = get_the_category();
You dont have to pass the post ID to it because it will default to the current $post->ID.
You can checkout that documentation here
Its also probably a bad idea to use $post in your foreach loop as that will overwrite the global $post variable. You may want to use something like $curPost
Related
I have initialized WP_Query object and using that object to show post information. But, when I go the permalink for any post, it showing the home page rather than that post page. Here is my index.php code:
<?php
$myWpQuery = new WP_Query(array( 'author_name' => 'me' ));
if($myWpQuery->have_posts()){
while ($myWpQuery->have_posts()) {
$myWpQuery->the_post();
?>
<?php the_title(); ?><br />
<?php
}
}
?>
I have three post with the author name "me". When I am loading the home page (http://localhost/wordpress/) its showing the title of those three posts inside proper anchor tag. But when I am clicking on the title, It's taking me to the post page (http://localhost/wordpress/hello-world/). Problem is here. This post page is also showing those three title as home page. But I expected only the title of the post that I clicked on.
But when I am using the simple following code it's working properly.
<?php
if(have_posts()){
while (have_posts()) {
the_post();
?>
<?php the_title(); ?><br />
<?php
}
}
?>
What's happening after initializing the WP_Query object. Could anyone explain it please.
Because you use the_post() in your query, you need to reset after to restore the global $post variable of the main query loop. The right way to do this when using WP_Query() is to call wp_reset_postdata() after your custom loop like this:
<?php
$myWpQuery = new WP_Query(array( 'author_name' => 'me' ));
if($myWpQuery->have_posts()){
while ($myWpQuery->have_posts()) {
$myWpQuery->the_post();
?>
<?php the_title(); ?><br />
<?php
}
// Restore original Post Data
wp_reset_postdata();
} else {
// No posts found
}
I'm not sure if this is causing your problem, but it's definitely something to fix. See https://codex.wordpress.org/Function_Reference/wp_reset_postdata.
I think you are missing a data setup. Take a look at the example below (taken from wordpress.org), that how your query should look like:
<ul>
<?php
global $post;
$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :
setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php
endforeach;
wp_reset_postdata();
?>
</ul>
https://codex.wordpress.org/Function_Reference/setup_postdata - here look at Example 1
https://wordpress.stackexchange.com/questions/99597/what-does-setup-postdata-post-do - information on what setup_postdata() function does
Without setting up postdata, your loop may store data from the previous iteration. The same would work with new WP_Query instead of get_posts().
I just started WP, and made a table with many rows arrived at WP house. But don't know how to show. basic content well shown but custom fields. learnt that a page have to be created to deal them in order to retrieve custom typed posts.
the following is from my content-movie.php under twentyfourteenchild:
/* translators: %s: Name of current post */
the_content();
// handling movie stuff starts
$p_id = $post->ID;
$ar_fields = array( 'studio','director','starring','grade');
.
.
foreach $ar_fields as $field
$some_field = get_post_custom_values($field, $p_id);
do some thing dealing $some_field...
end for
===================
In order to make a regular page to populate movie-typed custom posts, do I have to put such codes in archive-movie, page-movie single-movie etc ?
I guess, somewhere it can be dealt instead of putting same scripts in many files.
I really want someone to help me go right direction.
firstly create a custom page template by doing the following:
Copy the regular page.php and rename it to page-movies.php.
In the page header add the following php code:
/**
* Template Name: Movie Page
*/
Now the page template should be available in the backend, select the Movie Page as your page template.
Now replace the regular page query with your query, here is an example below:
<?php $args = array( 'numberposts' => 7, 'order'=> 'ASC', 'post_type' => 'movies');
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<div class="item">
<div class="item-content">
<?php if ( has_post_thumbnail() ) : ?>
<div class="thumbnails"> <a class="ajax" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'projects-thumbnail' ); ?>
</a>
<div class="copy">
<h3><a href="<?php the_permalink(); ?> ">
<?php the_title(); ?>
</a></h3>
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
This should do the trick...
I want to return the 2 newest posts when in single post mode, but exclude the current post. And I did it. The problem is, it just stopped working. It didn't change the code and it stopped on the server as well as on my localhost.
Here's the code:
<section id='recent_posts'>
<header class='recent_header'>
Recent posts
</header>
<?php
$id = $post->ID;
$recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id");
foreach( $recent_posts as $recent ) { ?>
<article class='single_recent'>
<header>
<?php echo $recent["post_title"]; ?>
</header>
<p>
<?php echo get_excerpt_by_id($recent["ID"]); ?>
</p>
</article>
<?php } ?>
</section>
Does anyone have an explanation ?
I tried removing the argument, still nothing. It returns an empty array.
Any suggestions which other function should I use to achieve the same effect ?
EDIT:
<?php
get_header();
get_sidebar();
?>
<?php the_post() ?>
<article class='post-single'>
<header class='post_header'>
<h1><?php the_title(); ?></h1>
<div class='post_header_bottom'>
<strong class='post_category'><?php echo get_the_category_list(', '); ?></strong>
<strong class='post_author'><span class='symbol'>U</span> by <?php the_author(); ?></strong>
</div>
</header>
<?php if (has_post_thumbnail()) : ?>
<figure class='post_single_image'>
<?php the_post_thumbnail(); ?>
<figcaption>No Will No Skill</figcaption>
</figure>
<?php endif; ?>
<div class='post_perex'>
<?php the_content(); ?>
</div>
<footer class='post_footer'>
<div class='post_footer_top'>
<div class='post_tags'>
<?php the_tags('', '', ''); ?>
</div>
<div class='post_time'>
<time datetime='<?php the_time('Y-m-d'); ?>' pubdate>
<span class='symbol'>P </span>
<?php relative_post_the_date(); ?>
</time>
</div>
</div>
<div class='post_share'>
<div class='share_show'>
<span class='symbol'>f</span> Like
|
<span class='symbol'>g</span> +1
|
<span class='symbol'>t</span> Tweet
<?php
if(function_exists('display_social4i'))
echo display_social4i("large","align-left");
?>
</div>
</div>
</footer>
</article>
<?php comments_template(); ?>
<section id='recent_posts'>
<header class='recent_header'>
Recent posts
</header>
<?php
global $post;
$id = $post->ID;
$qargs = array(
'post__not_in'=> array($id),
'posts_per_page' => 2
);
$recent_posts = new WP_Query($qargs);
if ($recent_posts->have_posts()) echo 'yes'; else echo 'nope';
if($recent_posts->have_posts()) : while($recent_posts->have_posts()) : $recent_posts->the_post(); ?>
<article class='single_recent'>
<header>
<?php the_title(); ?>
</header>
<p>
<?php the_excerpt(); ?>
</p>
</article>
<?php endwhile;endif; ?>
</section>
<div class='space'></div>
</div>
<?php
get_footer();
?>
You're missing your loop, or at least an global instance of the current post object. While I prefer to use the loop myself, you can get away with using the latter.
Change:
$id = $post->ID;
$recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id");
To:
global $post;
$id = $post->ID;
$recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id");
UPDATE:
The loop can be used in a single view to grab information from the default query_posts object. Even though it's just one post, the loop is most often used to populate the_content and other information.
You're correct that the ID should be irrelevant in this instance, since wp_get_recent_posts() should still return some results without any arguments (unless, of course, you're dealing with custom post types).
An alternate solution would be to try getting your recent posts using WP_Query:
<section id='recent_posts'>
<header class='recent_header'>
Recent posts
</header>
<?php
global $post;
$id = $post->ID;
$qargs = array(
'post__not_in'=> array($id),
'posts_per_page' => 2
);
$recent_posts = new WP_Query($qargs);
if($recent_posts->have_posts()) : while($recent_posts->have_posts()) : $recent_posts->the_post(); ?>
<article class='single_recent'>
<header>
<?php the_title(); ?>
</header>
<p>
<?php the_excerpt(); ?>
</p>
</article>
<?php endwhile;endif; ?>
</section>
WP_Query will default to the standard 'orderby'=>'date' and 'order'=>'DESC' parameters, so those don't need to be explicitly stated (though, you can add them if you like).
As mentioned above and in my comment, I'm unsure if you want just the most recent posts, or if you're trying to query recent custom post types. If the recent posts are of type 'post' and that's what you want, then that is the default for WP_Query's 'post_type' parameter as well as wp_get_recent_posts.
Otherwise, you will need to explicitly state the 'post_type' parameter, so that $qargs looks like:
$qargs = array(
'post__not_in'=> array($id),
'posts_per_page' => 2,
'post_type' => array('post', 'custom_post_type_slug', ...)
);
Just to check, you can also set 'post_type' to 'all' if you want to ensure SOMETHING is returned. If nothing is returned by either WP_Query or wp_get_recent_posts with 'post_type' set to 'all', then the issue is much larger, and I'll need to see all of the code in your single.php template.
Hope this helps.
NEW UPDATE:
Try commenting out the block of code altogether and substitute for:
wp_get_archives(array('type'=>'postbypost'));
If that doesn't work, then I'm fresh out of ideas. Something may have happened on your filehost's end that could possibly explain something happening out of nothing. Check and see if they have any announcements about this kind of activity. I know that many filehosts are in the process of replacing PHP4 and older versions of MySQL, which can probably cause some unforseen issues.
I would try creating a new subdomain with a clean, separate installation of Wordpress and a copy of your theme. Once that's set, just create a new post and see if the same problem occurs.
If so, then comment out chunks of code (possibly start with commenting out from get_sidebar through your first <article>...</article> block) in your single.php file and work out any errors that might pop up. If the block of code we worked on suddenly begins to populate, then post the block of code that was preventing it from happening and I'll try to work with you further (that is, if you're still having trouble).
Otherwise, if the clean installation fixes your issues, then it's probably some kind of discrepancy in your wp_options table. I would start merging tables over (first wp_posts, then wp_postmeta, then wp_terms....) until the problem pops up again.
Unfortunately, I think exhaustive testing might be in order to get this anomaly straightened out. Sorry I don't have a pure code solution. This is a pretty strange issue you're going through. Keep me posted, I'll do what I can to help.
Basically, I want to have two columns Wordpress page; one displaying video posts, and the other audio posts.
What's the easier way of doing this?
<div class="leftcol">
<?php $args = array( 'post_type' => 'video_post' ); //select only posts of type 'video_post'
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post(); ?>
<div class="post">
<h3><?php the_title() ?></h3>
<?php the_content() ?>
</div>
<?php endwhile; rewind_posts(); ?>
</div>
<div class="rightcol">
<?php $args = array( 'post_type' => 'audio_post' ); //select only posts of type 'audio_post'
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ?></h2>
<?php endwhile; rewind_posts(); ?>
</div>
I'm assuming you do know the basics of editing a theme, but if not, just say so! Alternatively, you could do the same thing using 'cat' instead of 'post_type' to separate your posts by category instead of two different types. I'd recommend using categories if you don't intend on adding any extra fields to enable your audio/video functionality.
I'm not sure if I understood you well, but I would do it with Widgets using Widget Logic plugin http://wordpress.org/extend/plugins/widget-logic/
If you want to hardcode your template you may need to check the query_posts() and reset_posts() finctions: http://codex.wordpress.org/Function_Reference/query_posts
Do you know how to use WordPress Codex? Do you know how the loop works? If yes you may want to use the second way, if not try to do it with the first option.
Good luck,
gezope
I want to have a page that shows all posts, separated by category. The idea is to get the categories, and then iterate through all posts for each category. The problem is complicated by the fact that I want to iterate through all posts of a given custom type, using a custom taxonomy as the categories. (Running Wordpress 3)
In my functions.php, my custom post type is registered as "video" and the custom taxonomy as "video_types".
In my custom page template that is supposed to show all videos arranged by category, this is the code that isn't returning any posts (and they're there, I checked):
<?php
$categories = get_categories(array(
'taxonomy' => 'video_types'
));
foreach ($categories as $cat):
?>
<section id="<?php $cat->slug ?>" class="video-category">
<?php
query_posts(array(
'cat' => $cat->cat_ID,
'posts_per_page' => -1
));
?>
<h2><?php single_cat_title(); ?></h2>
<p class="description"><?php echo category_description($cat->cat_ID); ?></p>
<?php while (have_posts()) : the_post(); ?>
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<article class="video">
<h3><?php the_title(); ?></h3>
<p>
<?php the_content() ?>
</p>
</article>
<?php endwhile; ?>
</section>
<?php endforeach; ?>
Jeez, once you figure out that each item of a custom taxonomy is called a term (not immediately obvious in the wordpress docs for the noob), its all much simpler to search for. This solution is easier to understand without all the custom query stuff.
<?php
// A term is an item of a taxonomy (e.g. "Promotional" could be a term for the taxonomy "video_type")
// ...so $categories could be $terms and it would still make sense
$categories = get_terms('taxonomy_name');
foreach( $categories as $category ):
?>
<section class="category-<?php echo $category ?>">
<h2><?php echo $category->name; // Print the cat title ?></h2>
<p class="description"><?php echo $category->description ?></p>
<div class="<?php echo $category->post_type ?>-list">
<?php
//select posts in this category (term), and of a specified content type (post type)
$posts = get_posts(array(
'post_type' => 'custom_post_type_name',
'taxonomy' => $category->taxonomy,
'term' => $category->slug,
'nopaging' => true, // to show all posts in this category, could also use 'numberposts' => -1 instead
));
foreach($posts as $post): // begin cycle through posts of this category
setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
?>
// Now you can do things with the post and display it, like so
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<?php
// Getting custom field data example
echo get_post_meta($post->ID, 'field_key', true);
?>
<?php the_content() ?>
</article>
<?php endforeach; ?>
</div>
</section>
<?php endforeach; ?>
And then any gaps in understanding can be filled by searching the functions above in the wordpress codex. In the above code, for my specific application, custom_post_type_name would be video, and taxonomy_name would be video_type (or video_types, I forget).
You might try another approach. Try using get_posts to get your posts sorted by your custom taxonomy, set up a variable that is initially an empty string (called $current_cat or something), and with each loop of the results, check the taxonomy and compare it with $current_cat - if different, print out a header for the new category and then the entry, if the same, print out an entry as usual.
A clear issue with your code (I believe) is that you're not properly querying your custom taxonomy. You should be using simply taxonomy_name => 'value' within your query, a custom taxonomy won't be touched by cat in a query.
Let me know if you need more detail.
edit: More detail!
// get a list of categories, in this case your custom taxonomy (your_taxonomy_name)
$querystr = "SELECT terms.* FROM $wpdb->term_taxonomy tax LEFT JOIN $wpdb->terms terms ON tax.term_id = terms.term_id WHERE tax.taxonomy = 'your_taxonomy_name'";
$categories = $wpdb->get_results($querystr, OBJECT);
foreach( $categories as $category ): // begin a loop through those terms (categories in your custom taxonomy)
echo '<div class="category-header"><h3>'.$category->name.'</h3>'; // print the cat title
echo '<p class="category-description">'.strip_tags(term_description($category->term_id,'your_taxonomy_name')).'</p></div>'; // cat description
$posts = get_posts( array( 'your_taxonomy_name' => $category->name, 'post_type' => 'your_post_type' ) ) //select posts in this category, and of a specified content type
foreach($posts as $post) : // begin cycle through posts of this category
setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
[ ... ] // do things with your post (display it)
endforeach;
endforeach;
That should do it - and this may be helpful for using get_posts.