I have a WordPress website with 2000 posts.
I need all the posts content to be updated with the title of the post at the end of the content.
For example ,the Post Titles
"Google Vs Amazon",
"Mturk",
"Cotton Shirts".,etc
The contents are:
"These 2 companies....",
"Its a freelancing work...",
"Shirts are stitched with fabric..", respectively.
The respective titles are to be added at the end of each post content.
But I don't want them generated each time the post is loaded, Instead I want the actual content to be updated like that. Preferably Mysql operation.
you have not asked a clear question but anyway as I understand you need some filters to change post content.
here is a sample code to add post title to post content.
add_filter( 'the_content', 'add_post_title_to_content' );
//the function that add filter to the_content have to use one variable.
function add_post_title_to_content( $content ) {
// here we check if it is in single post or main query. so this will not apply to other positions like feeds.
if ( is_single() || in_the_loop() || is_main_query() ) {
return get_the_title().$content;
}
return $content;
}
Related
I am looking for the way how to alter a WordPress post title and body when it is displayed to a normal user. But not in any other context, ie in admin module.
The task at hand is to fetch a query parameter, carry out some calls to external webservice and database operations and replace some text in both title and body. The <title> tag should also contain the changed title.
I do not look for a plugin solution, but rather were I should include a php file with all the code that does all the necessary processing.
My expectation was that I don't have to deeply meddle with the theme files. Of course, I could inject my code in the respective theme's page.php or content.php file. But I'd like to do it in less intrusive way, for example by including my functions_custom.php file in the top of functions.php, and this file would contain a collection of functions and add_filter() calls to hook those functions on certain events. Is that possible to achieve my goal that way?
This is my first attempt:
<?php
function alter_title( $title, $id = null ) {
return 'Altered title: ' . $title;
}
add_filter( 'the_title', 'alter_title', 10, 2 );
?>
This causes change in all the post titles in all contexts, including the post list in the Dashboard. But I need it to be altered only in view context (this also does not influence the text used in the <title> tag). Perhaps I can sniff this context somehow, and engage the alteration only when the view mode is detected?
While ago I needed to do similar thing and my fast and short workaround was to have an "API" (
Created a template for that specific post type and using PHP generated a post ID indicator:
$post = get_post(); echo "var post_id = '".$post->ID."';";
I created a custom API that "takes" $_POST request with "post_id" param and it would fetch adequate/custom (in your case a content that will be shown to the user) content from database for the post with the post_id from $_POST request.
Then for every post there would be an ajax request to that API which sends post_id and fetches all the needed info (in your case post title and body) and then using eg. jQuery
.append() would populate post title with response post title and body too, respectively.
Also, if you'd like to avoid a extra request than you should try to generate that 'user-visible' content with PHP. That would mean you need to collect the content before loading the page and echoit in the place of default content.
I suppose you should create a function in functions.php that takes an $post->ID and fetches title and body from the database. Then you should call that function on every loop and single post template, if you understand me.
is_admin() will return true if you are viewing an admin page.
So you could do this:
function alter_title( $title, $id = null ) {
if(is_admin()){
return $title;
}else{
return 'Altered title: ' . $title;
}
}
add_filter( 'the_title', 'alter_title', 10, 2 );
I have created a custom post type named bateau in functions.php. I also created a page where all the posts that belong to the bateau custom post type are displayed, showing some of the most important fields. Finally, when I click on one of those posts, a link sends me to the related custom post page, i.e. a particular type of boat.
The custom post bateau has its own custom fields, a thumbnail, and also its custom taxonomies.
I want to retrieve and display, when I'm on the page of a particular boat, not only the most important but all its custom fields, its thumbnail and its taxonomy.
So, in the functions.php, I have written this filter :
add_filter('the_content','add_text');
function add_text($text) {
global $post;
$text = "";
if($post->post_type == 'bateau') {
$text.= "<h1 class=\"bateau-entry-title\">".get_post_meta( $post->ID, 'bateau_nom', true )."</h1>";
return $text;
}
}
It works fine, provided that I don't write plain HTML text within closing and opening PHP tags, i.e. it only works if I wrap all the HTML in a PHP text var. If I don't do so, the content is also displayed at the beginning of the header, not once, but twice. Strange, isn't it ?
If I add this line :
$text.= "<img class=\"thumb\" src=\"the_post_thumbnail();
the thumbnail displays properly in the "article"...but, guess what, also at the beginning of the header, not once, but twice !!! I just can't find why the thumbnail behaves like this. Anyone can help please ?
In Wordpress, the page you called:
when I'm on the page of a particular boat
is called single. instead of putting a filter on the_content using
add_filter('the_content','add_text');
you would better add whatever you want to the loop. To do so, you may create a file called single-bateau.php (this is a protocol, so you need to name it exactly as this) in your current theme's root directory and there you may have your loop:
if(have_posts()){
while(have_posts()){
the_post();
the_title();
if( has_post_thumbnail() ){
the_post_thumbnail( $thumb_size );
}
the_taxonomies();
the_category();
the_tags();
}
}
I don't think you need the img tag if you're using the_post_thumbnail().
Try doing something like:
if ( has_post_thumbnail() )
the_post_thumbnail();
I have search.php in my WordPress theme, search result is generating fine using The Loop.
What I see is some post don't have content(blank post with title only), and they still appear in search result. Which is quite obvious, but I want only to list post which has some content.
If post don't have any content it should not list in Search Result even if its title has a Search Term.
I believe you won't need to see code to understand my concern. I tried searching for this answer on all known resources, nothing found exactly.
Down vote won't help me.
Inside your search loop try adding this:
global $post;
if ( $post->post_content != '' ) {
// Display this post because it has content.
} else {
// This post has empty content so do not display it.
}
Im trying to use a conditional statement on my single.php page.
What I want to do is, if it is of the Custom Post Type current-products, use a specific single -product.php template page, if not (ie a standard blog post) use the default single.php page.
I think Im using the right statement, but don't know what to do afterwards (to get the template to be used):
if ( is_single( 'current-products' == get_post_type() ) {
// If the post type is "Current Products" use this template...
// Please help me out on this bit
} elseif ( is_single() ) {
// If not, use the standard one...
// please help me out on this bit
}
I think that's right...?
WordPress automatically uses a different template page for different post types, if your post type is called products the files should be named single-products.php. You can read more about it from there Codex
In the same way single posts and their archives can be displayed using the single.php and archive.php template files, respectively,
single posts of a custom post type will use single-{post_type}.php
and their archives will use archive-{post_type}.php
where {post_type} is the $post_type argument of the register_post_type() function.
You can use is_singular() function, according to WordPress documentation: Ref Link
This conditional tag checks if a singular post is being displayed, which is the case when one of the following returns true: is_single(), is_page() or is_attachment(). If the $post_types parameter is specified, the function will additionally check if the query is for one of the post types specified.
True when viewing a post of the Custom Post Type book.
is_singular('book');
This will accomplish your task.
I believe if you want to use for posts try this:
is_singular('post');
Thanks
I am trying something out on wp. I want to display custom post types title on page post type.
here is my code.
function get_post_type_title(){
if('movie_ronny' == get_post_type()){ // if post type is movie_ronny, get post title
$movie = get_the_title(); //hold the post title in $movie.
}
if(is_page() ){ // if viewing page, display movie_ronny title
echo $movie;
}
}
add_filter('wp_head','get_post_type_title');
Above code does not display the title of movie post-type when viewing page. Any help is highly appeciated.
Your post type will most likely be shown on its own single-movie_ronny template. In which case of course you wouldn't need a conditional -
if('movie_ronny' == get_post_type()){...}
The other reason why this question is a bit strange is that your loop query must define within it's arguments what type of post, page or custom post type it is querying. So you would also then know which post type you are messing with and not need the first conditional.
And no matter what your situation is:
if(is_page() ){ // will only return true for pages. not posts or CPT's
Also,
get_the_title($id); // needs a post ID outside of the loop.
Well, there are two ways this function will fail (i.e. do nothing):
If the first if block is ignored then the second if block will not do anything (I believe it will actually show a notice, because $movie would not be defined).
If is_page() does not evaluate to true then nothing will be done in any case.
If you know anything about programming then this should be obvious. So, either you have no idea what you are doing, or I am misunderstanding something here.
Try this one:
<?php
$post_type = get_post_type_object( get_post_type($post) );
echo $post_type->label ;
?>