Title (<title>) of wordpress site is not proper - wordpress

In wordpress title of the page is divided by |. In my project On my front page title is fine (e.g. title | title). But in the inner pages title is not complete. Its not showing second part after | (symbol).
I have tried
<title><?php bloginfo('name'); ?> | <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>
But it does not show anything after | symbol (in inner pages).
I have also tried wp_title(). But its start showing url in the title instead of name/title. What else I can try to make my title correct

Your code
Prints the name of your website
Prints a pipe (|)
a. If it's the home page, it prints the description of your website (“Tagline” set in Settings > General)
b. If not, it prints the title of the current page
If it's printing {blog_title} | {blank} it most likely means something is wrong with the Page title. In the WP Admin, ensure the Page you're viewing has a Title set.
Alternatively, an SEO plugin is conflicting with the title function - try disabling plugins one by one and see if it impacts the output.
References:
Bloginfo, is_front_page, wp_title

From codex:
<?php
if ( ! function_exists( '_wp_render_title_tag' ) ) :
function theme_slug_render_title() {
?>
<title><?php wp_title( ' ', true, 'right' ); ?></title>
<?php
}
add_action( 'wp_head', 'theme_slug_render_title' );
endif;
?>
Since Version 4.1, themes should use add_theme_support() in the functions.php file in order to support title tag, like so:

I have fond a solution with the help of THIS POST
Just did little bit changes in the solution of that post and its working fine now.
<title><?php if (is_front_page()) { bloginfo('name');echo ' | ';bloginfo('description'); } elseif ( is_category() ) { single_cat_title(); echo ' - ' ; bloginfo('name'); }
elseif (is_single() ) { single_post_title(); }
elseif (is_page() ) { bloginfo('name'); echo ' | '; single_post_title(); }
else { wp_title('',true); } ?></title>

Related

Removing URL from displaying in get_the_content

I'm building out my theme and running into an issue where YouTube URLs are showing up in what I'm using as the excerpt. I currently have a conditional that will show a trimmed down content IF there is no excerpt. Here is my template code:
<?php
if ( ! has_excerpt() ) {
echo wp_trim_words(wp_strip_all_tags( get_the_content(), 40 )) ;}
else {
the_excerpt();
}
?>
In this particular example, https://imgur.com/EdLdInW
the post has a YouTube Gutenberg block as it's first block and showing a trimmed and stripped the_content. It's pulling the YouTube URL which I don't want.
Currently on Wordpress 5.1.1 with Understrap framework. Any help would be great!
You can remove the URL by using preg_replace:
$string = "The Third Culture: The Frontline of Global Thinking http://someurl.com/;via #edge";
$regex = "#(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)#";
echo preg_replace($regex, ' ', $string);
So in your case it will be like this:
<?php
if ( ! has_excerpt() ) {
$content = wp_trim_words(wp_strip_all_tags( get_the_content(), 40 )) ;
$regex = "#(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)#";
echo preg_replace($regex, ' ', $content);
}else {
the_excerpt();
}
?>

Visual Composer not showing specific page styles[Product Category]

I have a page build with Visual Composer and i want to show this page content to other page(product category), it not working.
I used code:
Vc_Manager::getInstance()->vc()->addShortcodesCustomCss($page_id);
in this solution: Visual Composer not showing specific page styles
Step 1: Build page A with id = 100
Step 2: View content of page A into page B (other page with id=105), it works well!
Step 3: View content of page A into page C (product category with term_id=30), not working, vc_custom_XXX not css output
I feel like I must be missing something here? can anyone give me some solutions. Thank
My code, it working!
$content = get_post($id)->post_content;
$content_css = visual_composer()->parseShortcodesCustomCss( $content );
if ( ! empty( $content_css ) ) { ?>
<style type="text/css" data-type="vc_shortcodes-custom-css">
<?php echo strip_tags( $content_css ); ?>
</style>
<?php }
It working, Good luck.
$postID = 633;
echo do_shortcode(get_post_field('post_content', $postID)); // show the_content by ID
$shortcodes_custom_css = get_post_meta( $postID, '_wpb_shortcodes_custom_css', true );
if (!empty($shortcodes_custom_css)) {
echo '<style type="text/css">' . $shortcodes_custom_css . '</style>'; // show css in visual composer
}

Display content if i'm in the post

I hope i will be clear with my explaination . Is it possible to show for exemple a simple text if the user is in the post .
For instance, i show in my case a simple text if the user is in the template category, but i want to display also this text if the user is in the content of the post :
I need with a function because it's in the header.php file .
With this function, i can show the simple text "Hello World" if i'm on http://mywebsite.com/category/ and what about http://mywebsite.com/the-name-of-the-post ?
<?php if (basename($template) == "category.php")
{
echo 'Hello World';
}
?>
Thanks guys, i hope it's clear (sorry for my english)
You should use WordPress Conditional Tags:
if ( is_single() || is_page() ) ) {
// If it's a single post or a single page echo 'Hello Single!'
echo 'Hello Single!';
}
if ( is_archive() && is_category() ) {
// If it's an archive page for any category, echo 'Hello Category!"
echo 'Hello Category!';
}
Read more about Conditional Tags on the WordPress Codex.

Page title before site name on Wordpress

I have a WP site and am trying to figure out how to place the name of the page before the site name. Essentially, I'd like to change "ABC Inc. | About Us" to "About Us | ABC Inc.". Surprisingly, I ended up empty-handed after googling. Is this addressed by some plug-in?
You can do this in your themes header.php file by replacing your tag with the following:
<title><?php wp_title('|'); bloginfo('name'); ?></title>
http://codex.wordpress.org/Function_Reference/wp_title
It is common to see usage of
<title><?php wp_title('|'); bloginfo('name'); ?></title>
or similar uses on the <title> tags; but according to the WordPRess Codex, it should not be used:
The wp_title() function should not be used by a theme in conjunction
with other strings or functions (like concatenating with
bloginfo('name')) to write the content of the element, because
it will render plugins unable to rewrite the whole title in case the
plugins use the wp_title filter do the rewrite, which is the best
practice. The use of this function is now a requirement for theme
developers.
The best way is to include a filter in your functions.php file:
function filter_wp_title( $title ) {
$site_description = get_bloginfo( 'description' );
$filtered_title = $title . get_bloginfo( 'name' );
$filtered_title .= ( ! empty( $site_description ) && ( is_home() || is_front_page() ) ) ? ' | ' . $site_description: '';
return $filtered_title;
}
For more info on filtering the title, see the codex.
If you want to use a plugin, check out WordPress SEO by Yoast. Highly recommend it. You can drill down to writing custom titles for posts, categories, tags, homepage, etc.
Note: if it appears not to be working with your theme, click on "force rewrite titles".

how to determine is_home in wordpress if query a category?

I need to query a category in home page. in index.php file I used this script
$all_featured_posts = query_posts(array('category_name'=>'featured-programs'));
Then in the header.php file I need to change the title
<title>
<?php
if ( is_home() ) {
echo 'My site name' ;
} elseif (is_404()) {
echo '404 Not Found';
} elseif (is_category()) {
echo ' Category' . wp_title('',0).' | My site name' ;
}
?>
The problem is when I query a category in the index file then the is_home return false ( Tried with is_front_page() also ) Then it alway show the title with the name of the category which I query.
How I can fix it? Thanks you!
I might be wrong, but I think because you use query_posts(), all your is_* functions change their values. And, well, because you do query a category, is_home() should return false.
What you can do to solve it, is use new WP_Query(), and get all the posts from it. This way, you will not be affecting the original WP_Query, and thus the is_* functions.
The code should look like this:
$query = new WP_Query('category_name=featured-programs');
while ( $query->have_posts() ) : $query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();

Resources