<?php $query = new WP_Query('posts_per_page=1&'); ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<?php the_title(); ?><br />
<?php echo paginate_links( $args ) ?>
<?php endwhile;?>
<?php wp_reset_postdata(); ?>
Here is the query I'm using, it should display the first post title then displays the pagination links, however it only shows the post title without the pagination links. What's wrong?
The paginate_links function should go outside the loop and you should use the query variable "paged"
Please see this link http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html for more information on using this.
Related
I want to include the content of an existing page in the header.php file? What's the easiest way to do this? I am guessing that there is a way to load an existing page via a php call, however I am not sure of the correct syntax.
Thanks, in advance.
You could instantiate a new query:
$query = new WP_Query( 'page_id=7' );
After doing so, you make a loop to display the query's content.
<?php if ( $query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); // Important, so this loop does not affect the global post object afterwards ?>
<?php endif; ?>
This is based on WP's official codex: https://codex.wordpress.org/Class_Reference/WP_Query
I am unable to fetch post tags added from wordpress admin panel. I am using fishpig magento extension and everything is working perfectly.
I am fetching posts using the code
$posts = $this->getPosts();
and I need tags associated with each post.
Please help.
You can get the tags for an individual post by calling the getTags method on the post object. Here is a snippet pertaining to post tags from the post view template:
<?php $tags = $post->getTags() ?>
<?php if (count($tags) > 0): ?>
<span><?php echo (count($categories) == 0) ? $this->__('This post was tagged with') : $this->__('and was tagged with') ?> </span>
<?php $it = count($tags) ?>
<?php foreach($tags as $tag): ?>
<a href="<?php echo $tag->getUrl() ?>">
<?php echo $tag->getName() ?>
</a><?php if (--$it > 0): ?>, <?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
You have a collection of posts rather than a single post, you can call getTags on an individual post as you are iterating over your collection.
foreach($posts as $post) {
$tags = $post->getTags();
}
I want to display a list of post titles from a custom taxonomy in a particular order.
I thought the best way to control the order would be to add a custom field and sort on that custom field.
The problem I'm having is that I'm trying to use the built-in functionality of Wordpress and I can't find a way to add sort functionality.
My Scenario looks like this
The calling url is ...com/taxonomy/term
This calls up a template, the filename of which is taxonomy-taxonomyname-term.php
My template is simply the index.php template renamed and edited to contain this loop
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<ul>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?> etc
This displays a list of titles but I can't find a way to control the order of the titles.
The only way I've seen to set the order of a group of posts is to define the order of the posts in the query. But of course in this case I dont have a query because I already have the posts via the calling url.
Is there any way to add sort functionality without adding another query or is the query mandatory.
Suppose your custom fields is my_date
You can create custom query like this.
query_posts('meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC');
To use it
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<ul>
<?php /* Start the Loop */
query_posts('meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC');
?>
<?php while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?> etc
For more info http://wpengineer.com/1915/sort-posts-custom-fields/
I have the code below for a category in WordPress. It displays the name of the category when I want it to be displaying the title of the post. How do I get it to display the title of the post proper.
<? query_posts('category_name=implants');
?>
<h3><?php single_cat_title(); ?></h3>
<?php if (have_posts()) : while (have_posts()) :
the_post(); ?>
<?php the_content( __('Read the
rest of this page »', 'template')); ?>
<?php endwhile; endif; ?></p>
Do not use query_posts unless your intention is to modify the
default Wordpress Loop. Use WP_Query instead for standard Wordpress
queries.
Look at your code. You're calling single_cat_title(). It means
exactly what it looks like: You're pulling the title of the queried
category. You want to call the_title() to grab the post title.
Not as important as the above, but your opening tag is <? rather
than <?php. You should make it a habit of specifying your server-side language to avoid potential future problems, even though it might not be initially apparent.
Here's what your revised loop should look like:
<?php
$query = new WP_Query('category_name=implants');
if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php
the_content( __('Read the rest of this page »', 'template'));
endwhile; endif;
wp_reset_postdata();
?>
i want to show 4 posts in page and after 4 posts it will show next button to read next posts.
<?php while (have_posts()) : the_post(); ?>
<?php
global $post;
$myposts = get_posts('numberposts=4&order=ASC&category=3');
foreach($myposts as $post) :
setup_postdata($post);
?>
here is content with html
<?php endforeach; ?>
<?php endwhile; ?>
<?php endif; ?>
You just need to add paged parameter to your query_posts:
$myposts = get_posts('numberposts=4&order=ASC&category=3&paged' . get_query_var('paged'));
And add post_nav_link() to display next and prev link.
<?php posts_nav_link(); ?>
Cheers, oh, btw, you can post specific wordpress question at wordpress.stackexchange.com.