How to Hide few post from the Wordpress home page? - wordpress

I am creating a Lyrics website with 3.9.1 wordpress version and I want to create a post for each Album and its songs with internal links.
What am doing right now is creating a post of albums and pages for songs. But if I do that, I am not able to give tags for pages ( songs ) for SEO.
So I want to create even songs with postings so that I can add tags to them but I don't want songs to be listed on my home page unless the visitor clicks on the link in the album.
How can I fix this?

Tags to Pages
If you want to add Tags for Pages you can by doing this:
add_action( 'init', 'tags_for_pages' );
function tags_for_pages() {
register_taxonomy_for_object_type( 'post_tag', 'page' );
}
Just Posts
But if you wanna do all the things just with posts, I think you should:
1) Create 2 Categories: Albums and Songs
2) Create The Albums Post and Add the Album Category to it
3) Create the Songs Post and Add the Song Category to it
4) Add this code to your functions.php
add_action( 'pre_get_posts', 'modify_home_query' );
function modify_home_query( $query ) {
if ( !is_admin() && $query->is_home() && $query->is_main_query() ) {
$query->set( 'category_name', 'albums' );
}
}
and in your home page will be only posts with the category albums, you will have to paste the links in the content of the Album Post, like you would if it was a page.
Best Choice
But I think you should search for Custom Post Types, and how to create it.
You could create a Custom Post Type Albums and a Custom Post Type Songs and link them using plugins like Advanced Custom Fields, Posts 2 Posts and / or CPT-Onomies
Post Type Generator: http://generatewp.com/post-type/

You can make your homepage query only look for albums.
For this to work, you need to set posts categories "Album" or "Song".
Then, you need to find your theme home page php file, like a index.php, and look for your current query. It looks like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//some code
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Then add this before
<?php query_posts('cat_name=album'); ?>
and this after
<?php wp_reset_postdata(); ?>
This will only work if you use categories!!

Related

How can I add some posts in the homepage and have a blog page as well?

I am building my own - custom - theme in WP.
In my index.php, I have many static contents ( Don't know how to convert them into dynamic fields in WP yet, but I will learn this later ).
The issue is that I want to display a number of posts in the homepage 2 posts and below them a link to redirect the user to a blog page which displays the whole posts.
I searched a lot and I found a solution which is not doing what I want, it was about setting a static front-page and set the blog page as the Posts page under settings then reading.
After doing so, the whole static content - that was in my index.php doesn't appear on the homepage anymore and the homepage becomes an empty page and the whole content went to the blog page, which made me CRAZY!!
All I want to do; is to keep my static content and 2 of my most recent posts in the homepage and create a blog page which contains the whole posts.
Hope to find a solution for this Without Plugins.
Add This Snippet Code into your front-page.php or wherever you want to show 2 posts recently published.And Also a Blog Page Link to navigate the user to all blog posts..
$the_query = new WP_Query( array( 'post_type' => 'post',
'posts_per_page' => 2,
'orderby' => 'publish_date',
'order' => 'DESC'
)
);
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
the_excerpt();
the_author();
// add whatever you want
endwhile;
wp_reset_postdata();
echo 'See All Blog Posts Here : <a href='.site_url("blog").'>All Blog Posts</a>';
else :
esc_html_e( 'Sorry, no posts yet published.' );
endif;
?>
This is going to be a detailed answer to my problem to help anyone who might face the same problem, it will cover everything in steps for a WordPress newbie, like me! :D
As mentioned in my Question, my problem is that I want to display 2 posts in my index.php and make a separate blog page contains the whole posts.
The Most Detailed Solution:
1) Go to your index.php and use the normal posts loop:
<?php
// The Loop
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>
2) From WP Dashboard, go to Settings then Reading and change the Blog pages show at most value to 2 posts Or whatever number of posts you want.
Now, we've done the first step and show only a specific number of posts in our index.php, let's continue the steps to display the whole posts in our blog page.
3) Create a PHP Page called page-blog.php.
Notice: if you don't have a page.php you have to create it, for more information about how to create a custom PHP Page, I recommend #Adam's answer here.
4) Go to your WP Dashboard and create a new page called blog.
5) In your page-blog.php you need to add this snippet:
// Posts Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 6, 'paged' => $paged );
query_posts($args);
// The Loop
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} else {
echo "Sorry, There is no posts to be displayed";
} // end if
?>
Notice: We used the same loop as we did before in index.php, only the new 2 lines of codes which above the loop is the new different thing here in our page-blog.php page
6) I set the posts_per_page to 6 because it is my blog page and I want to display more posts there. Set its value to whatever number you want, it depends on you
7) BOOM, it works and you did what you wanted, just like me!!
This problem seemed VERY COMPLICATED for me, but when I searched and read more information about this, I found that it is a VERY EASY to solve problem, and I hope this answer help you if you faced it!

how to show the data of CPT UI plugin on website?

I have used CPT UI to add some posts with taxonomies. I have filled two post data in CPT UI for practice. Now I want to show these post on a page. What all code I have to write.
You can use Wp_Query along with the post name that you created using CPT Ui plugin to display those posts. Like, e.g. i had created a post named as school then code to display all posts of School type is as following :
$query = new WP_Query( array( 'post_type' => 'school' ) );
while($query->have_posts()):
$query->the_post();
echo $query->ID; // it will print the ID of post
endwhile;
Hope this will clear the things..
In order to pull in the custom fields/post meta, you will need to write some code within the WordPress loop (https://codex.wordpress.org/The_Loop) in your template file.
The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. Any HTML or PHP code in the Loop will be processed on each post.
eg.
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
For all post meta:
$meta = get_post_meta( get_the_ID() );
echo '<pre>';
print_r( $meta );
echo '</pre>';
Or for a single value:
$custom_field_value = get_post_meta( get_the_ID(), 'custom_field_key_name', true );
See below for more information in the WordPress Codex:
https://codex.wordpress.org/Custom_Fields
and
https://developer.wordpress.org/reference/functions/get_post_meta/

how to retrieve custom fields on taxonomy page?

I am using the "advanced custom fields" plugin and need to have it so that a custom field is pulled in for the category pages. I can get these to come in on pages, but the category pages are giving me a lot of trouble... 'video' is the name of the custom field I want to pull in.
This is the code I am currently using:
<?php echo get_field('video', 'clear-creek'.$wp_query->queried_object->term-4); ?>
or just a standard version like this which works on the regualar pages...
<?php the_field('video'); ?>
but it's not working... can someone please help steer me in the right direction?
Thanks!
If you are on a category's archive page, you would use this:
<?php echo get_field('video', 'category_'.get_query_var('cat')) ?>
If you are on a custom taxonomy instead, you would use this:
<?php $queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
echo get_field('video', $taxonomy . '_' . $term_id); ?>
This will dynamically get the taxonomy's slug and ID, and build out your get_field based on that information.

Wordpress category

This is code from my wordpress homepage template. There are 3 same code, like this one bellow. The question is how can I put different categories to show different news when I post. At the moment code is same on my homepage and it show only category named World, it's the first category created. How can I setup the rest of code with different categories.
Currently it's like this : http://zaslike.com/files/os8hvxg38oendfwcvrz6.png
I need to put other categories instead same three.
http://justpaste.it/e0nh - Here is the code of homepage template.
I believe you are asking for this:
<!-- Your Query -->
<?php // The Query
query_posts( array ( 'category_name' => 'change-to-category-name', 'posts_per_page' => 1 ) ); ?>
<?php // The Loop
while ( have_posts() ) : the_post();?>
// YOUR HTML GOES HERE
<?php endwhile; ?>
<?php // Reset Query
wp_reset_query(); ?> // DON'T FORGET THIS
Use this code at the parts of the site that display your categories change the categories name to the ones you want to display and you are good to go.
Good luck welcome to SO.

query_posts does not return old posts?

I'm using query_posts on my home page to fetch articles from multiple post types. I'm using following code to do so.
<?php query_posts( array('post_type' => array('post','page','custom_post1','custom_post2','custom_post3','custom_post4','custom_post5','custom_post6')));
if (have_posts()) : while (have_posts()) : the_post(); // begin the Loop ?>
/*html code goes here*/
<?php endwhile; endif; ?>
It fetches the posts rightly, but the problem is: it does not fetch old posts. So for example I have 1 post and its publish date is 02/july/2012, this post won't be displayed on my home page. But as soon as I update the date to 02/Aug/2012 it starts showing up on Home page.
So is there any code I can add in query_posts that will enable older posts to be fetched too.
Wordpress by default fetch 10 posts or number of posts set by user from admin.
query_posts accept argument that return number of record.
query_posts( 'posts_per_page=5' );
query_posts( 'posts_per_page=50' );

Resources