Wp_list_pages change page title on output - wordpress

I am using wp_list_pages to create a submenu on a page and child page.
All working fine
My menu looks like
Parent, page title is Hello
Child,
Child,
Child,
etc
I am trying to find a way to dynamically change the Page title on output.
In my example above, I would like my Parent page to display GoodBye instead of Hello.
You might wander why I don't just rename my page to Goodbye.
It is because the Page title , in my design, is displayed in 3 different format
- menu Header Hello displays Welcome (can change this via WP menu
- Page title display the correct title, ie Hello
I need my left menu to display Goodbye....
hope this makes sense for somebody
thx

Use a custom field on your page...let's call it sidebar_title.
Then, you'll need to convert your wp_list_pages code into a custom WordPress loop (there might be a way to use get_pages to do the same if you prefer that.
Here's some sidebar code to list the current page and it's child pages, replacing the_title(); with your sidebar_title if it exists. It's pretty ugly...the main point is to show you how to access custom fields.
<?php
//Get children of current page and display with custom fields.
//You will probably need to adjust this.
$args=array(
'post_parent' => $post->ID,
'post_type' => 'page',
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
?>
<ul>
<?php
// Print parent with sidebar_title, if it exists
$sidebar_title = get_post_meta($post->ID, 'sidebar_title', true);
if ($sidebar_title != ''){ ?>
<li><?php echo $sidebar_title;?></li>
<?php } else { ?>
<li><?php the_title(); ?></li>
<?php } ?>
<?php
// Print each child page with sidebar_title, if it exists
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php
$sidebar_title = get_post_meta($post->ID, 'sidebar_title', true);
echo $sidebar_title;
if ($sidebar_title != ''){ ?>
<li><?php echo $sidebar_title;?></li>
<?php } else { ?>
<li><?php the_title(); ?></li>
<?php } ?>
<?php endwhile; } ?>
</ul>
<?php wp_reset_query();?>

Related

After initiating the WP_Query object, all the post permalinks are showing the same page

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().

How to show wordpress CPT items in the single page

I defined custome post type (Slug = portfolio).
After a Portfolio post publishes and I click on View post, page not found displays. This is my first problem.
I want when user clicks on a portfolio item it opens in an another page with more details of information. This is my another problem.
This is the code of template page for showing portfolio posts (the name of the page is: portfolio.php)
<?php
/* Template Name: Portfolio */
$arg = array('post_type'=>'portfolio');
$loop = new WP_Query($arg);
while($loop->have_posts()):
$loop->the_post();
?>
show post
<br>
<?php
endWhile;
?>
and i have single-portfolio.php with these codes:
<?php
$arg = array('post_type'=>'portfolio');
$loop = new WP_Query($arg);
while($loop->have_posts()):
$loop->the_post();
?>
show post
<br>
<?php
endWhile;
?>
please help me
The contents of your single-portfolio.php code could be something like this.
<?php
while ( have_posts() ) : the_post();
the_title();
the_content();
endwhile; // End of the loop.
?>

Wordpress latest posts featured image from category

quick Wordpress question.
I want to display the last 30 posts from my category "photos", but to only display the featured image on the relevant page as a link that will take the user to the actual post.
I have managed to do this, but it displays posts from all categories, not the "photos" category. Code I'm using is below.
I'm sure it's simple, but would love to know how to display just the recent posts from the photo category only (as the featured image).
Thanks
<!-- In functions.php -->
function recentPosts() {
$rPosts = new WP_Query();
$rPosts->query('showposts=100');
while ($rPosts->have_posts()) : $rPosts->the_post(); ?>
<div class="photos">
<li class="recent">
<?php the_post_thumbnail('recent-thumbnails'); ?>
</li>
</div>
<?php endwhile;
wp_reset_query();
}
<!-- this is on the page template -->
<?php echo recentPosts(); ?>
Just add the category selection to your query.
Replace:
$rPosts = new WP_Query();
$rPosts->query('showposts=100');
With:
$rPosts = new WP_Query('category_name=photos&showposts=100');
Reference: http://codex.wordpress.org/The_Loop#Exclude_Posts_From_Some_Category
You need to provide the loop argument that you want post only of certain category by providing category id cat=1. Replace 1 with the id of your photos category
<!-- In functions.php -->
function recentPosts() {
$rPosts = new WP_Query();
$rPosts->query('showposts=100&cat=1');
while ($rPosts->have_posts()) : $rPosts->the_post(); ?>
<div class="photos">
<li class="recent">
<?php the_post_thumbnail('recent-thumbnails'); ?>
</li>
</div>
<?php endwhile;
wp_reset_query();
}
<!-- this is on the page template -->
<?php echo recentPosts(); ?>
Add the category ID to your query arguments. echo on the last line is redundant by the way. Your function outputs the HTML directly rather than returning it.
Finally your original markup was invalid. An li can't be the child of a div so I've corrected that in my example.
function recentPosts() {
$rPosts = new WP_Query( array(
'posts_per_page' => 30,
'cat' => 1
'no_found_rows' => true // more efficient way to perform query that doesn't require pagination.
) );
if ( $rPosts->have_posts() ) :
echo '<ul class="photos">';
while ( $rPosts->have_posts() ) : $rPosts->the_post(); ?>
<li class="recent">
<?php the_post_thumbnail( 'recent-thumbnails' ); ?>
</li>
<?php endwhile;
echo '</ul>';
endif;
// Restore global $post.
wp_reset_postdata();
}
<!-- this is on the page template -->
<?php recentPosts(); ?>

ACF PostObject return title and permalink

I have some code, namely -
<?php while (has_sub_field('services_featured_links')) {
$postObjects = get_sub_field('services_link');
if($postObjects){ ?>
<ul class="intro-menu">
<?php foreach($postObjects as $post){
setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php
}
wp_reset_postdata();
}
?>
</ul>
<?php } ?>
I have 4 post objects in my repeat fields - at the moment, utilising this code returns a lot of empty li and a tags. It also returns the parent page as a link.
I basically just need to get the permalink and title of each post object I have selected.
Any help please?
$postObjects is not what you think it is.
ACF returns the value of the field called services_link, if that's just a field then you probably have a string inside $postObjects.
If this is for a repeater block, and I'm willing to guess it is, then you need to loop through and initialise it with the_row().
I'm assuming services_featured_links is the name of your repeater, services_links is the name of the field you use for the link within that repeater, and I'm also assuming you have a services_title field. A lot of assumptions there.
My guess is you'll want something along the lines of the following:
<?php if( have_rows('services_featured_links') ){ ?>
<ul class="intro-menu">
<?php while (have_rows('services_featured_links')) { the_row();
$services_link = get_sub_field('services_link');
$services_title = get_sub_field('services_title');
<li><?php echo $services_title; ?></li>
<?php } ?>
</ul>
<?php } ?>

Load page content and blog post index content (WordPress)

I have a question about Wordpress' template structure and querying posts.
I have my templates setup where the (for example) archive-$posttype.php is built like:
get_header();
$args = 'page_id=18'; //Client Uses page
query_posts($args); the_post();
get_template_part( 'sub-header' );
// Reset Query
wp_reset_query();
?>
<div class="content">
<?php get_template_part( 'loop' ); ?>...
I do this to set my default $post variable for my sub-header.php file which prints out content from that page:
<div id="subheader">
<h1><?php echo get_post_meta($post->ID, 'header_title', true)?></h1>
<?php echo get_post_meta($post->ID, 'header_description', true)?>...
However, using this method on the home.php template, doesn't work:
get_header();
$temp_query = $wp_query;
$page_id = 119; //Client Uses page
#$post = get_page( $page_id );
$args = array('page_id' => $page_id);
$post = new WP_Query( $args ); the_post();
get_template_part( 'sub-header' );
wp_reset_postdata();
?>
<div class="content">
<?php get_template_part( 'loop' ); ?>
<?php get_sidebar( 'news' ); ?>
</div><!--.content -->
<?php get_footer(); ?>
I'm curious why this works on one template and not on the home template. AND, am I going about this the wrong way? What's the correct way to have page content in the sub-header template that in most cases is ACTUALLY related to that current page the user is on.
Thanks!
Not sure I understand your problem exactly but if you are doing what I think, having a chunk of text from a specific page above another loop that pulls in posts of some sort or another I would use the template naming structure.
<?php /* Template Name: My Template */ ?>
This will allow you to use the standard loop to get content from whatever page the clients sets the template to (avoiding the static ids you are using).
Your home page bug could be due to it not being set as the posts page in the reading settings. As I understand it if a page is not set as the posts page it will act like a normal page unless you rewrite the query specifically.
"What's the correct way to have page content in the sub-header template that in most cases is ACTUALLY related to that current page the user is on."
Rather than trying to tie together two unrelated pages/posts, it might be easier to use a custom field tool. My current favorite is Advanced Custom Fields.
With ACF, you can add supplemental fields (image, wysiwyg, file upload, 14 total field types) to your posts and pages, then easily pull the custom data into your template. It's very well documented and extremely easy to use.
So, I changed the way my sub-header.php template works. Basically some basic checking on what type of page/post was being set/called then dynamically pulled the relevant page info.
<?php
if (is_page()) :
$header_title = get_post_meta($post->ID, 'header_title', true);
$video_id = get_post_meta($post->ID, 'youtube_video_id', true);
$thumbnail = get_the_post_thumbnail($post->ID, 'post-thumbnail', array('class'=>'visual-element'));
$description = get_post_meta($post->ID, 'header_description', true);
elseif (is_home()) :
$page_id = 119; // News page
$page = get_page( $page_id );
$header_title = get_post_meta($page->ID, 'header_title', true);
$video_id = get_post_meta($page->ID, 'youtube_video_id', true);
$thumbnail = get_the_post_thumbnail($page->ID, 'post-thumbnail', array('class'=>'visual-element'));
$description = get_post_meta($page->ID, 'header_description', true);
elseif (is_archive()) :
$page_id = 18; // Client Uses page
$page = get_page( $page_id );
$header_title = get_post_meta($page->ID, 'header_title', true);
$video_id = get_post_meta($page->ID, 'youtube_video_id', true);
$thumbnail = get_the_post_thumbnail($page->ID, 'post-thumbnail', array('class'=>'visual-element'));
$description = get_post_meta($page->ID, 'header_description', true);
endif;
?>
<div id="subheader">
<h1><?php echo $header_title; ?></h1>
<?php if ( $video_id ) : ?>
<iframe class="visual-element" width="300" height="200" src="http://www.youtube.com/embed/<?php echo $video_id;?>?rel=0" frameborder="0" allowfullscreen></iframe>
<?php elseif ($thumbnail) : ?>
<?php echo $thumbnail; ?>
<?php endif; ?>
<?php echo $description; ?>
</div><!-- #subheader -->

Resources