I'm attempting to fetch the subtitle from the PARENT page and echo the text within a title tag.
Function:
<?php
$id_to_get = $post->ID;
if ( $post->post_parent ) {
$id_to_get = $post->post_parent;
}
$parent = $id_to_get;
$title = get_the_title($parent);
$subtitle = get_post_meta($id_to_get, '_base_page_subtitle', true);
?>
HTML Implementation: ...title="<?php echo $subtitle; ?>">
Unfortunately, it is not successfully grabbing the subtitle text. I'm able to populate the title echoing the variable $title, just not with the variable $subtitle.
The value for the Subtitle option is stored within the table "_base_page_subtitle"; I'm using options framework.
I know my issue is with the if statement, however, being new to PHP, I'm having a bit of difficulty figuring out how to capture the data and display it properly.
Thanks again of time!
you can do it without a function - just echo it straight out
title="<?php if ( $post->post_parent ) { echo get_post_meta($post->post_parent, '_base_page_subtitle', true);}else{echo get_post_meta($post->ID, '_base_page_subtitle', true);}?>"
Related
I'm working on a small function for a WordPress site, and can't figure why my function is echoing the data on my webpage.
My function is:
function get_post_titles($args) {
$titles = array();
$posts = new WP_Query($args);
while ($posts->have_posts()){
$posts->the_post();
$titles[] = the_title();
}
return $titles;
}
and I call it in a separate .php file:
<?php
$titles = get_post_titles($args);
?>
<select>
<option selected="selected">Choose an Article</option>
<?php
foreach($titles as $title){
?> <option> $title </option><?php //this isn't working, pls ignore for this post
}
?>
</select>
...
The result is a webpage with a Select box (yay!) but for some reason, the page also shows ALL of the titles in a row (it outputs the full array).
I would expect that if I had say, echo get_post_titles($args), but nowhere in my code do I ask it to echo. Why is my array visible?
I think it's because of my $titles = get_post... line. But again, why is it displaying $titles instead of simply declaring/assigning the variable?
(I know there's a WordPress SE site, but I think this is more directly a PHP issue than WP issue. However, if I should post over there instead, let me know).
the_title() displays the title. You should use get_the_title() instead.
From Wordpress Documentation, following is the usage of the_title:
the_title( $before, $after, $echo );
$echo
(Boolean) (optional) Display the title (TRUE) or return it for use in PHP (FALSE).
Default: TRUE
You can do the following (set the third argument to false):
$titles[] = the_title('','',false);
I have a shortcode that is supposed to return the title and content for multiple custom posts on a single page. It correctly displays the title for each post, but when it comes to displaying each post's content, it only displays the content from the first post. What am I doing wrong?
Figuring out how to get the content from within a shortcode has been tricky enough, so if anyone has any suggestions, I'd appreciate it!
My code is:
if($customposts->have_posts() ) : while ($customposts->have_posts() ) : $customposts->the_post();
$post= get_the_ID();
$foo = get_the_title();
$bar=do_shortcode($content);
echo "<h2>".$foo."</h2><p>".$bar."</p>";
endwhile; endif;
It doesn't look like you need to us do_shortcode. If title is working correctly, you should be able to assign get_the_content() to your $bar variable.
$bar = get_the_content();
Since you're looping through posts, store the html from each iteration of the loop, and then return all of the html at once (remember, you can't echo or print from a shortcode callback... well, you shouldn't, or you'll get some unexpected results):
$html = '';
if($customposts->have_posts() ) :
while ($customposts->have_posts() ) : $customposts->the_post();
$post= get_the_ID();
$foo = get_the_title();
$bar = get_the_content();
$html .= "<h2>$foo</h2><p>$bar</p>";
endwhile; endif;
return $html;
Also, be careful about using $post as your own variable, you will undoubtedly come into conflict with other scripts, including core.
The code below is displaying the title in black text before it displays blue title with hyperlink under it.
I only want the link to appear.
if ( $query2->have_posts() ) {
// The 2nd Loop
while ( $query2->have_posts() ) {
$query2->the_post();
if ($post->ID == $do_not_duplicate)
continue;
$permalink = get_the_permalink($query2->post->ID);
$ID = $post->ID;
$titleAtribute = the_title_attribute();
$title = get_the_title();
echo '<h2 id="post-' .$ID.' ">
<a href="'.$permalink.'" rel="bookmark" title="Permanent Link to '.$permalink.' ">
'.$title.'</a></h2>';
}
// Restore original Post Data
wp_reset_postdata();
}
For example, on my website: http://skkelti.cz/, the following text appears in black above the link with the same text:
-Martin Davídek ml. : „Fanoušci jsou vždy to, co vás žene kupředu“-
Where is this coming from and what do I need to do to stop it from appearing?
The problem is with the_title_attribute(). This is displaying the value directly instead of returning it.
The function accepts echo in $args to specify whether to display or return the value. The default value is true (to display it), so pass false to return the value e.g.:
$titleAtribute = the_title_attribute('echo=0');
I have added a custom meta box, once i update the data, it saved successfully. But now i am trying to fetch using this code, but it is not getting content of meta box saved. Please help me out.
<?php
global $post;
$code = get_post_meta( $post->ID, 'caption_code_footer', true );
if($code != ''){
echo $code;
}
else { ?>
I am lorem Ipusm Text
<? } ?>
This code appears okey, so you need debug a bit....It's sound like $post->ID has wrong value... try echo it. echo $post->id; or all the object var_dump($post);
Also Check If you arent in right context. For example in category context you need to use the loop...
I think something error on update your meta box please try this..
$footer=$_POST['caption_code_footer'];
update_post_meta($post_id,'caption_code_footer',$footer);
for echo the value updated..
get_post_meta($post->ID, 'caption_code_footer', true);
So i have the single-portfolio.php which present one of my project.
This function makes proper title to my projects every time I choose one.
<h1><?php the_title(); ?></h1>
Now whatever project i choose it always transport me to the Angela...
<h1><?php the_title(); ?></h1>
What i want to do is to have proper link to the proper project in the title.
I figured sth like this but it does not work.
<?php
$f = "http://facebook.com/";
$t = "http://twitter.com/";
$l = "http://linkedin.com/";
if (the_title()=='Facebook') {
Echo "<a href=$f> Facebook</a>";
} elseif (the_title()=='Twitter') {
Echo "<a href=$t> Twitter</a>";
} else {
Echo "<a href=$l> Linkedin</a>";
}
?>
What i get on the page is 3 times written Facebook if its Facebook page or 3 times Twitter e.g:
FacebookFacebookFacebook(the only last one "Facebook" is a link)
The problem is that the_title() echos the title, rather than returning it (as documented in the Codex). That means that, for each of your if conditions, the title gets echoed out.
Try using get_the_title() instead - this returns the post title rather than echoing it.
the_title() is a function that returns the title of the current page/post in wordpress. For the FB/Twitter/LinkedIn portion of your code you should just include "Facebook" instead of using the_title()
Use a custom field in your post to store the link in the post. Call the field "url" for example:
now you can read the field in your template and use it:
<?php $url = get_post_meta( get_the_ID(), "url", true ); ?>
<h1><?php the_title(); ?></h1>