WordPress - Put URL which displays all sticky posts - php

I am creating a new theme for my blog where I checked some posts as a Sticky post from wp-admin and on front-end I have given some CSS to highlight those sticky post.
Now I want to give link on that highlight area which redirect to particular page having all sticky posts.
I also want to do the same for other post formats as well, like IMAGE, LINK, etc.
Can someone help me on this?

You can do it with custom wordpress template and query.
Create custom page template for each post format lists page Like for sticky posts list, create page template page-sticky.php
Inside this page add custom query with loop of posts something like below :
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_format' => 'sticky', 'posts_per_page' => 10, 'paged' => $paged );
query_posts($args);
if ( have_posts() ) : while (have_posts()) : the_post();
get_template_part( 'content', get_post_format() );
endwhile;
endif;
wp_reset_query();
You can change sticky word with any post format you want. also make sure you put everything in php quote.

Related

wp AJAX load more plugin repeating the same previous posts

I using wp ajax load more plugin While clicking the button it repeating the same previous post. How to fix it. Here I share my code in below:
<?php
$the_query = new WP_Query( array(
'posts_per_page'=>10,//on loading page i show 10 after click load more i want to show other posts
'post_type'=>'post-name',
'category_name' => 'A-E',
'orderby'=> 'title',
'order' => 'ASC',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1)
);
?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
// here I print the following data
<?php
endwhile;
?>
<?php
echo do_shortcode('[ajax_load_more post_type="post-name" posts_per_page="10" category="a-e" button_label="Load More"]');
?>
can anyone fix it?
I just use this to solve my own problem:
while ( $query->have_posts() ) : $query->the_post();
$do_not_duplicate[] = $post->ID; // Store post ID in array
// Other loop actions could go here
endwhile; wp_reset_query();
$post__not_in = ($do_not_duplicate) ? implode(',', $do_not_duplicate) : '';
echo do_shortcode('[ajax_load_more post__not_in="'. $post__not_in .'" post_type="post-name" posts_per_page="10" category="a-e" button_label="Load More"]');
If you want to get the next set of pages with ajax-load-more you must use the offset parameter. The ajax-load-more plugin must get the next posts that WP_Query has. So both WP_Query and ajax-load-more must query the same pages. First alter the shortcode to be same as WP_Query, by adding order and orderby parameter:
[ajax_load_more post_type="post-name" posts_per_page="10" order="ASC" orderby="title" category="a-e" button_label="Load More"]
Then add offset=(get_query_var('paged') ? get_query_var('paged') : 1)*10 like this:
[ajax_load_more post_type="post-name" offset='.((get_query_var('paged') ? get_query_var('paged') : 1)*10).' posts_per_page="10" order="ASC" orderby="title" category="a-e" button_label="Load More"]
Note: Seems like ajax-load-more doesn't know of - so you must separate category or post_type by ,. Unless you have actually a category named a-e
You don't need to create a custom query. You are using a plugin that will generate it for you via their shortcode. As per their documentation, you can just create your settings for the shortcode.
See instructions here https://connekthq.com/plugins/ajax-load-more/docs/shortcode-builder/
Just take the generated shortcode and paste it into your page/post or in your php file with do_shortcode.
The plugin handles the query and the posts per page etc.

Custom tag.php template

I'm very new to PHP and have some trouble wrapping my head around it sometimes so please bear with me.
I have a lot of categories and a lot of tags. I started making category-slug.php templates but it'd probably be best for me to just use category.php and tag.php templates. I just can't get them to work unless I add in something like 'category_name' => 'art'. I've also read that querying isn't ideal (I think that's what I'm doing?), but I have had custom development done and I'm not sure if that has or hasn't been left as my only option.
$page_content = "";
if (have_posts()) :
while (have_posts()) : the_post();
$page_content .= get_the_content();
endwhile;
endif;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'post', 'posts_per_page' => 6, 'paged'
=> $paged );
And then later on I have this with post title, date, excerpts, etc. to follow.
<?php
$wp_query = new WP_Query($args);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
How do I make the category.php and tag.php pages specific to each unique slug without having to manually make each one?
Category Template Hierarchy, as described in WP codex:
The Template Hierarchy specifies that WordPress will use the first Template file it finds in your current Theme's directory from the following list:
category-slug.php
category-ID.php
category.php
archive.php
index.php
You don't have to do any custom WP_Query to get the posts of category.
if (have_posts()) :
while (have_posts()) : the_post();
the_content();//this is the content of the single post, belonging to this category
the_title();//title of the single post
the_excerpt();//excerpt of the single post
//and so on
endwhile;
endif;
It's the similar scenario for tags as well.
More details on Category Templates and Tag Templates.

Remove Title Link for Specific Category in Wordpress

I'm running Wordpress 4.1. I have two blog pages on my site, and though I don't really know php, I've done some tinkering and figured out how to modify the page templates so each page only displays posts for a specific category. That bit of code looks like this:
<?php query_posts('cat=2'); ?>
That works fine. Page A displays posts from category 1, and Page B displays posts from category 2.
What I'd like to do is disable post title links for one specific category. In other words, Page A would display posts from category 1 (with standard clickable title links), and while Page B would display posts from category 2 (with non-clickable title links).
I'm an HTML/CSS guy, so really out of my depth here, but if there's a way to modify the loop to achieve this, I'd love to learn how. Thanks in advance for any help.
Yes, you can do this using the category.php theme file. When this page is hit, it loads a specific category requested and the posts that fall into that category.
Your theme and loop may look something like this:
<?php single_cat_title(); ?>
<?php echo category_description(); ?>
if (have_posts()) : while (have_posts()) : the_post();
/// display posts from specific category
endwhile; endif;
Or if you don't want to use that page which is designed for that, you can create your own loop:
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => 50 ) );
All together like this:
<?php
/* retrieve unlimited # of posts with an category slug of music */
query_posts( array ( 'category_name' => 'music', 'posts_per_page' => -1 ) );
// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;
// the Loop
while (have_posts()) : the_post();
the_content( 'Read the full post ยป' );
endwhile;
?>

how to display posts from all page in wordpress

Currently I am trying to develop wordpress one page portfolio theme. Now it shows blog posts and page posts. I have created a custom page template for displaying one page portfolio items. It will display post from those pages which user will create from theme menu.
Now I need to create that, but I have no idea how to do that. Please note that it will display only those page posts which user created from theme menu , and it will show those page navigation link(although I dont need any help about that , but I need help to show pages post) , and another thing is that , it will not display blog posts.
Run a WP_Query on your index page, and inside the loop add the page contents
<?php $args = array(
'post_type' => 'page', // Calling pages only
'order' => 'ASC',
'posts_per_page'=> '-1', // display all pages published
);
$loop = new WP_Query( $args ); if( $loop->have_posts() ): while( $loop->have_posts() ): $loop->the_post();?>
<?php the_title(); //Page Contents etc.?>
<?php endwhile; endif; wp_reset_postdata();?>

Wordpress - Display Limited Post From A Specific Category

In wordpress i need to display post on home page from a specific category.
Need Five Post With Title
Category Is News
Display On Main home page
i use plugin business news but when i activate this plugin nex gen gallery plugin will be conflict now i dont know how to resolve this please suggest.
Following is the code to display post of a specific category. To display on home page, you will have to make following changes in your index.php file.
$query = new WP_Query( array('category_name'=>'news', 'posts_per_page'=> '5') );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
endif;

Categories