targeting title in wordpress post - wordpress

I am working on a wordpress plugin that modifies the title of a post. I only want to do this when I am viewing a single post. To be specific, I want to add a link beside the title, but for purposes of the question, I will be adding some arbitary text.
I started out by using the 'the_title' filter hook, and calling this function.
function add_button_to_title($title)
{
global $post;
if(is_single())
{
return $title.'googly googly';
}
return $title;
}
The problem is, the links on the side bar apparently also use 'the_title', as I saw my text showing up in the side bars as well, which led me to:
if(is_single() && in_the_loop())
But then, in my theme(and i suppose themes in general) there is a link to the previous post and next post, which also uses 'the title' filter. So finally I have:
if(is_single() && in_the_loop() && ($post->post_title == $title))
The last conditional basically makes sure that it is the title of the post that is being printed, not the title of the next or previous post. This works but I am not sure how well it will work given different themes...It seems like terribly hacked together. Any advice from wordpress gurus out there? I am worried that the title would be modified for other reasons and the conditional will fail.
Any help is appreciated!

Ying,
There isn't really a good solution except, as ShaderOp said, requiring theme modification. Your solution will work for the most part. The only exception is if the theme developer has changed the query in a page. I'd say this is probably a good enough solution that it'll cover more than 95% of the cases you'd run into.

I solved a similar issue by adding a check to see if the title being filtered matches the title of the post. This avoids the issue with other post titles on the page (in sidebar, menu) also getting filtered.
function add_button_to_title( $title ) {
global $post;
if( is_single() && $title == $post->post_title ) {
return $title . 'googly googly';
} else {
return $title;
}
}

Wouldn't it be easier to keep the original version of your add_button_to_title function, but instead of hooking it to a filter, call it directly from your single.php page in the appropriate place?
For example, somewhere in your theme's single.php, instead of this:
<h3 class="storytitle">
<?php the_title(); ?>
</h3>
Use this:
<h3 class="storytitle">
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php echo add_button_to_title(the_title('', '', false); ?>
</a>
</h3>

today I ran into a similar problem. the_title gets called several times accross the whole page (e.g., in the html-head, the menus, the sidebar). I followed a similar approach using conditionals and the post/page id.
Additionally, I added a boolean flag which is set to true using the 'the_content' filter. So the title gets changed until the content is displayed. This way, I ensure that sidebars/widgets are not affected (e.g. Thematic theme has a default widget with links to pages - here the other conditionals would not be helpful as get_the_id() would return an equivalent). This ONLY works if the theme uses sidebars on the right. I did not find a way yet how to hook in directly before the 'the_title' call for the page/post to enable the boolean flag.
function myplugin_adjust_title($title, $id) {
global $myplugin_title_changed;
if ($myplugin_title_changed) {
return $title;
}
if (in_the_loop() && is_page('myplugin') && $id == get_the_ID()) {
$title = '';
}
return $title;
}
add_filter('the_title', 'myplugin_adjust_title', 10, 2);
function myplugin_adjust_title_helper_content($content) {
global $myplugin_title_changed;
$myplugin_title_changed = true;
return $content;
}
add_filter('the_content', 'myplugin_adjust_title_helper_content');

Related

how to remove excerpt on category archive page in genesis framework?

I am trying to remove the excerpt ( content ) from the category archive page so only the title shown on the page, not the content.
I am currently trying the code below with no luck?
does anybody have any suggestions?
add_action ( 'genesis_before_entry' , 'designody_remove_entry_content_archives' );
function designody_remove_entry_content_archives() {
if (in_category('5')) {
remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
}
}
my second solution... also not working
function replace_content( $output) {
if (is_category('5')) {
return '' ;
}
}
add_filter( 'get_the_excerpt', 'replace_content' );
I don't have a fully satisfactory Genesis-specific solution, and I'd be curious to know what StudioPress itself says, but I do have WordPress solutions.
First, to use the Genesis hook and a condition, you need to wrap it in a function:
To quote the Codex: "Warning: You can only use conditional query tags after the posts_selection action hook in WordPress (the wp action hook is the first one through which you can use these conditionals). For themes, this means the conditional tag will never work properly if you are using it in the body of functions.php, i.e. outside of a function."
https://codex.wordpress.org/Conditional_Tags
The following works to remove the excerpt on the specified category archive page:
add_action( 'loop_start', 'remove_content_on_category_pages' ) ;
function remove_content_on_category_pages() {
if ( is_category( '5' ) ) {
remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
}
}
However, the in_category() conditional - which should help with entry content when it appears in other archives (categories, blog, etc.) - doesn't seem to work.
In order to get the desired results as I understand them - no post content, in this implementation "the excerpt", for this category on any type of archive page, but preserving it for individual posts - you can revert to the trusty the_content filter, but note that the the_content can be a tricky and powerful tool subject to unexpected secondary effects. That said, on a vanilla Genesis installation, the following tests out:
add_filter( 'the_content', 'test_gen_content' ) ;
function test_gen_content( $content ) {
/**
apply to all secondary queries don't apply if single page or post, or in
admin, otherwise obliterate - can use is_single() instead of is_singular()
if only posts are at issue
**/
if ( ! is_singular() && ! is_admin() && in_category( '157' ) ) {
return '' ;
}
return $content ;
}
I must say, however, that, given the peculiarities and complications of the Genesis framework, and the peculiarity of the request, out there in the wild I'd be very tempted to use a CSS kludge, shaped to requirements - at least failing direct instruction or intervention from a Genesis maven or StudioPress itself. Whether you needed to refine the CSS further to suit your particular installation, I can't say, but it might be a lot easier, actually, then sorting out the PHP even with the correct hooks etc.
//substitute appropriate category slug for "development-forums"
.home .category-development-forums .entry-content,
.blog .category-development-forums .entry-content,
.archive .category-development-forums .entry-content
{
display: none;
}

hatom-entry errors on the Google snippet test

Almost sure that I'm not the first one that has this question but when I test my (WordPress) page on Google Snippet Test Tool I got hatom-enty errors like:
hatom-feed
hatom-entry:
Fout: At least one field must be set for HatomEntry.
Fout: Missing required field "entry-title".
Fout: Missing required field "updated".
Fout: Missing required hCard "author".
I found some tutorials about this but that is for standard WordPress themes. I'm using Inovado from ThemeForest and I can't figure out in which file I have to edit this data.
Someone familiar with this?
I also got problems with snippet review... Good in testresults but doesn't show up in de Google Search Results. Don't know why...
You can Add this code to the function.php file in your theme's directory and it will solve the problems.
//mod content
function hatom_mod_post_content ($content) {
if ( in_the_loop() && !is_page() ) {
$content = '<span class="entry-content">'.$content.'</span>';
}
return $content;
}
add_filter( 'the_content', 'hatom_mod_post_content');
//add hatom data
function add_mod_hatom_data($content) {
$t = get_the_modified_time('F jS, Y');
$author = get_the_author();
$title = get_the_title();
if(is_single()) {
$content .= '<div class="hatom-extra"><span class="entry-title">'.$title.'</span> was last modified: <span class="updated"> '.$t.'</span> by <span class="author vcard"><span class="fn">'.$author.'</span></span></div>';
}
return $content;
}
add_filter('the_content', 'add_mod_hatom_data');
The code contains 2 functions. The first function will use the WordPress filter hook for “the_content” to add the class of “entry-content” to the article. To add in the other essential hAtom fields, the second function will add a short sentence to the end of your post article, which contains updated time, post title and author, with required microdata.

Single Page Navigation Menu Dynamically Generated

hHi all! I have posted this question on the WP support forums, but the community doesn't seem to be as active as stack's, so I am taking a chance here!
I am looking for a plugin that would automatically create a navigation menu (through the use of shortcodes for example) on a long single page documentation page.
The long page is divided into sections. I can imagine using a shortcode at the beginning of every section, and this will create a menu that would be displayed in a sidebar for example (called through a second shortcode perhaps, or a widget)
Any thoughts? Advice?
Thanks!
Use [section]Section Title[/section] shortcodes, then [section_navigation] where you want the navigation links output.
This works, but with a massive caveat -- that [section_navigation] needs to be in your post/page after the other [section] shortcodes... otherwise it generates an empty list.
You should be ok to use it in your theme by putting <?php echo do_shortcode("[section_navigation]");?> in sidebar.php. It will work as long as get_sidebar() is after the_content() in your theme templates (it usually is).
This to go in functions.php
$whit_sections = "";
// [section]My Section Title[/section]
function whit_section_shortcode( $atts, $title = null ) {
// $content is the title you have between your [section] and [/section]
$id = urlencode(strip_tags($title));
// strip_tags removes any formatting (like <em> etc) from the title.
// Then urlencode replaces spaces and so on.
global $whit_sections;
$whit_sections .= '<li>'.$title.'</li>';
return '<span id="'.$id.'">'.$title.'</span>';
}
add_shortcode('section', 'whit_section_shortcode');
// [section_navigation]
function whit_section_navigation_shortcode( $atts, $title = null ) {
global $whit_sections;
return '<ul class="section-navigation">'.$whit_sections.'</ul>';
}
add_shortcode('section_navigation', 'whit_section_navigation_shortcode');

Including Custom Fields & static text in the_content in WordPress

I am editing a Custom Post Type template, and am using custom fields to enter info into a meta box to be included on the page, as well as include some static default text on all the pages.
I basically need to "chunk" together the post info in the_content along with the static text and some meta box info. Here's what I want:
the_content
static text
meta box 1
more static text
meta box 2
end of the _content
I have plugins that add social buttons before the_content and a signature after the_content so I am trying to figure out how to get all my custom stuff sandwiched in between those.
If I just add the meta boxes i nthe template, they display outside of the_content and the plugins display in unwanted places.
I ended up figuring this out on my own. The solution: using functions.php and add_filter, I had to create a new function to create the default content, and it works great.
here's the general code for anyone interested:
function custom_post_type_default_content($content) {
global $post;
if ($post->post_type == 'your-custom-post-type') {
$content .= '<p> '. get_post_meta( $post->ID, "metabox-1-slug", true ).'
<br />
<p> '. get_post_meta( $post->ID, "metabox-2-slug", true ).'</p>
<p>YOUR TEXT HERE.</p>';
}
add_filter('the_content', 'custom_post_type_default_content', 0);
Note that the zero just near the end controls placement. I have a social media plugin that has a priority of "1", and to get the default content to appear above that I have to make this a priority of "0".
Also note the single apostrophes that open and close the code following $content .=
You basically add whatever you want between those apostrophes, and in this case I am pulling metabox info which have their own apostrophes containing code. It gets confusing!
In other words, your code should be $content .='YOUR CUSTOM CONTENT' and within those apostrophes, add your text, code, etc. The standalone metabox code is '. get_post_meta( $post->ID, "metabox-1-slug", true ).' which is nested inside where the YOUR CUSTOM CONTENT text is.
I am basically explaining this to myself, as these were the things that tripped me up so figured would explain them in detail to help someone else like me. Or me when I have to go look this up again!
Post your single.php here or on pastebin along with the custom field names you're using (& where you want them) and I'll try to help you figure out what you want.
Copy this code to your function.php file.
function content_function_update($content) {
global $wp_query, $post;
if ($post->post_type == 'your-custom-post-type') {
$postid = $wp_query->post->ID;
$value1 = "your value 1";
if($value1 !== '') {
$content = $content . "<br>" . $value1
}
else {
$content = $content;
}
}
return $content;
}
add_filter('the_content', 'content_function_update');
Add any custom content in variables and append it to $content variable.

wordpress: previous_post_link() / next_post_link() placeholder

I am having trouble with the previous_post_link() and next_post_link() functionality. When there is no previous post, the function previous_post_link() does not display a link, likewise for the next_post_link() and the last post. I would like to have a placeholder image so that the design stays consistent.
Currently I have images of green arrows pointing left and right, I would like to place an image of a grey arrow if there are no more posts to go back to.
Is there a way to use the next_post_link()/previous_post_link() functions but not have the link removed.
I also wonder if there is a way for the links to cycle, so that if you come to the most recent post, the next post link would bring you back to the first post.
************ UPDATED ************
Here is the code, based on "silent's" advice (accepted answer) to use get_adjacent_post():
<?php
if(get_adjacent_post(false, '', true)) {
previous_post_link('%link','<img src="larr.gif"/>');
}
else {
echo '<img src="larr2.gif"/>';
};
if(get_adjacent_post(false, '', false)) {
next_post_link('%link','<img src="rarr.gif"/>');
}
else {
echo '<img src="rarr2.gif">';
};
?>
So you can "capture" what next_post_link() and previous_post_link() return using ob_start() and ob_get_clean(), then apply a conditional to it.
Code in practice:
$previous_string = "<-Back";
ob_start(); // start output buffering
previous_post_link("%link", $previous_string);
$previous_link = ob_get_clean(); // stop output buffering and store
if ($previous_link == '') {
echo '<span style="color: #ccc">' . $previous_string . '</span>';
} else {
echo $previous_link;
}
I never try this myself. However, you may refer to this post. It uses get_adjacent_post().
Why cant you try the below?
<div style="float:right"><?php if(get_previous_posts_link()) { previous_posts_link('Newer Entries »'); } else { echo 'No Newer Entries'; } ; ?></div>
Alternatively to display Next and Previous Post Links to Blog post you can easily do that. This is very well explained at globinch.com (WordPress Tips : How to Add Next and Previous Post Links to Blog? )
http://www.globinch.com/2010/10/12/wordpress-tips-how-to-add-next-and-previous-post-links-to-blog/

Resources