I came across a tutorial on the internet for creating a sitemap in WordPress. It does what I want - it lists all pages and posts on the website however I was wondering if it was possible to exclude a page from the sitemap. In this case I want to exclude the sitemap link. Is it possible to do this? I have included the code for the sitemap below.
<?php
/*
Template Name: Sitemap
*/
get_header(); ?>
<?php if ( have_posts() ) : while( have_posts() ) : the_post();
the_content();
endwhile; endif; ?>
<h2>Pages</h2>
<ul>
<?php
// Add pages seprated with comma[,] that you'd like to hide to display on sitemap
wp_list_pages(
array(
'exclude' => '',
'title_li' => '',
)
);
?>
</ul>
<h2>Posts</h2>
<?php
// Add categories seprated with comma (,) you'd like to hide to display on sitemap
$cats = get_categories('exclude=');
foreach ($cats as $cat) {
echo "<ul>";
query_posts('posts_per_page=-1&cat='.$cat->cat_ID);
while(have_posts()) {
the_post();
$category = get_the_category();
// Only display a post link once, even if it's in multiple categories
if ($category[0]->cat_ID == $cat->cat_ID) {
echo '<li>'.get_the_title().'</li>';
}
}
echo "</ul>";
}
?>
<?php get_footer(); ?>
I am aware that the comments within the code give me some idea as to where to list the pages I want to hide however I am unsure as to how to do this. If anyone can help me it would be much appreciated.
Many Thanks.
If you want to exclude pages from your Site Map you have to write here
wp_list_pages(
array(
'exclude' => 'ID1, ID2',
'title_li' => '',
)
);
ID1 and ID2 it's your page id on WordPress.
Related
I want to display a list of categories after the first post in the Loop of index.php (this is the template my WP theme uses to display posts).
I've searched around on the web and found some code (see below) which is supposed to do as I want - inject a list of category titles as links between a list of posts in the Loop.
However, it is not working as expected. It only shows one category title, not all of them. Interestingly, it displays the title of the first post's category (the post that comes before the custom code), but no others.
My Loop code, including the custom code I inserted, is as follows:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php get_template_part('content'); ?>
// START CUSTOM CODE
<div>
<?php
if( $wp_query->current_post == 0 ) {
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= ''.$category->cat_name.''.$separator;
}
echo trim($output, $separator);
}
}
?>
</div>
// END CUSTOM CODE
<?php endwhile; ?>
Hoping someone can help.
Thanks,
Mekong
It is a little unclear to me from your question, but it seems like you want a list of all categories, correct? I think the line "$categories = get_the_category();" is getting categories for the current (in this case first) post only.
If you want a list of all categories that exist in your blog/website try 'get_categories', https://developer.wordpress.org/reference/functions/get_categories/
Try this code, small change in your code...
<?php if (have_posts()) : $i = 1; while (have_posts()) : the_post(); ?>
<?php get_template_part('content'); ?>
<div class="categories">
<?php
if( $i == 1){
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0
) );
foreach ( $categories as $category ) {
printf( '%2$s<br />',
esc_url( get_category_link( $category->term_id ) ),
esc_html( $category->name )
);
}
}
?>
</div>
<?php $i++; endwhile; ?>
I've been searching all morning for answers to no avail. I'm building a one page WordPress template. I have a home page which uses a one page template called one-page.php that is brining in all the other pages. Heres the php from that template:
<?php
$args = array(
'post_type' => 'page',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; endif; ?>
This code works great. All the sections can use the content-page.php template part, excluding the portfolio section which I would like to use another template part that brings all the portfolio custom post types.
I've tried to add conditional if statements to both the one-page.php and the content-page.php, like this:
<?php if ( is_page( 'portfolio' ) ) : ?>
//My portfolio custom post type loop is here
<? endif; ?>
But that didn't work either - I think that is because the is_page() function will be checking the current page being displayed which is the Home page. Rather than figuring out what page the query is currently dealing with - but I'm not sure.
Can anyone help me understand how I would go about conditionally loading the portfolio section into a separate template part?
You can achieve this checking page slug, which you can get in the loop. If it is "portfolio" (or whatever you saved), load content-portfolio.php, otherwise content-page.php. Something like this:
if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
if ("portfolio" === $post->post_name) {
get_template_part('content', 'portfolio');
} else {
get_template_part('content', 'page');
}
endwhile; endif;
Set condition in file
<?php
if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
if (is_page('portfolio')) {
get_template_part('content', 'portfolio');
} else {
get_template_part('content', 'page');
}
endwhile; endif;
?>
create a template file named content-portfolio.php which is copy of content-page.php and put the below code in it. if it show 'call' it means your template is working.
portfolio.php
<?php
echo "portfolio.php";
die('Call');
?>
at the moment I am querying out all pages that I have in WordPress and am displaying them on a page. Like this:
<?php
query_posts(array('post_type' => 'page'));
while(have_posts())
{
$this_page = the_post();
echo the_content();
}
?>
The question is, how can I do the same thing but just for one specific post? As a side note, if I need to use id's of pages where can I find them, as at the moment I don't see what id page has in WordPress.
take a look at the page on query_posts in the wordpress codex here: http://codex.wordpress.org/Function_Reference/query_posts .
Adjust arguments accordingly. For example, to query a post with a specific id:
query_posts( 'p=5' );
something like this?
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );//al items from my-category-slug
while(have_posts())
{
$this_page = the_post();
echo the_content();
}
Well I can't figure this one out...
I have this Wordpress I use as a photo gallery blog.
I have a basic setup using the main default loop for posts.
Like this:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//the post
<?php endwhile; ?>
<b>Not Found</b>
<?php endif; ?>
In the sidebar and where ever, I want to appear random posts.
I've managed to do that. With this:
<?php query_posts($query_string . 'showposts=1&orderby=rand'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//the post
<?php endwhile; endif; ?>
It looks amazing! In theory.
There are duplicate posts all over the place. And that just looks stupid.
I have read lots of articles but I just can't seem to get it to work :(
Any help would be much appreciated.
Try this code for random post.
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
Or You can get help from this url mention below
http://codex.wordpress.org/Template_Tags/get_posts
After a good night of sleep, here's what I have done:
Creating array with post ID:
<?php $already_posted = array(); ?>
The Main loop where at the end I record the post ID to array:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//the post
<?php $already_posted[]= $post->ID; endwhile; ?>
<?php else : ?>
<b>Not Found</b>
<?php endif; ?>
And the random post code using post__not_in to avoid duplicates and again recording post ID:
<?php $args = array( 'numberposts' => 1, 'orderby' => 'rand', 'post__not_in' => $already_posted );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
//the post
<?php $already_posted[]= $post->ID; endforeach; ?>
Works evertime!
You can do amazing stuff with this :)
Thanks to paislee and Arvind Pal for helping out.
Skip would-be duplicates by remembering displayed ID's from the first loop
$displayed = array(); // create an array that we'll use associatively
In your first loop, each time:
$displayed[get_the_ID()] = TRUE; // <-- save all post IDs in here
Change your random loop opening like this:
<?php if (have_posts()) : while (have_posts()) : the_post();
// skip post IDs you've already seen
if ($displayed[get_the_ID()]) continue;
?>
Due to randomness in the number of duplicates, you may want to alter your query so that it gets all posts, and change the second loop to break once the desired number of random posts is reached.
Notes
showposts is depracated. Replace showposts=1 with posts_per_page=-1
Hello I want to Create a home page that has updating blog entries.
So 4 lists of headlines from different categories
And I want to have a link to the regular blog page with a different template.
Right now I just changed index.php around to have the containers for the featured posts content.
So this is a two part question how do I get these mini updates for the thing
I want to use query_posts() multiple times I assume and separate by category.
And how do I make a linkable page to a blog.php file which currently is telling me that all these functions are undefined.
<?php get_header(); ?>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>
If you want to create a page that includes WP data/posts outside the themes or blog folder, you need to include and make available the Wordpress functions first:
define('WP_USE_THEMES', false);
require('./blog/wp-blog-header.php');
And then you can make the queries for each set of posts by category:
$args = array( 'numberposts' => '5, 'offset'=> 1, 'category' => 'your category ID' );
$myposts = get_posts( $args );
foreach($myposts as $post) {
...some code...
}
I hope it helps.