WordPress.org

Forums

[resolved] Conditional page title for home verses other (13 posts)

  1. rhonorginal
    Member
    Posted 2 years ago #

    Hello

    I'm trying to create a conditional page title.

    For home, it would display the blog name, for anything else, the post/cat name plus blog name.

    This is as far as i've got. Can this all be within the <title> instead of having two?

    <?php $subs = get_categories('parent=9');
    $sub_ids = array();
    if( $subs ) foreach( $subs as $sub ) { $sub_ids[] = $sub->term_id; }
    if( is_single() && in_category( $sub_ids ) ) : ?>
    <title><?php bloginfo('name'); ?></title>
    <?php else : ?>
    <title><?php wp_title(''); ?> — <?php bloginfo('name'); ?></title>
    <?php endif; ?>
  2. Chandan Kumar
    Member
    Posted 2 years ago #

    You can use as follows:

    <?php
    global $post;
    $blogname = bloginfo('name');
    
    if(!is_home()){
     $blogname .= $post->post_title .' - '. $blogname;
    }
    echo '<title>'. $blogname .'</title>';
    ?>
  3. rhonorginal
    Member
    Posted 2 years ago #

    would that not drop in the post title before <title>?

  4. Chandan Kumar
    Member
    Posted 2 years ago #

    The above code display page title under title tag. Please try once.

  5. rhonorginal
    Member
    Posted 2 years ago #

    I did try it. It pus the post title into the html before the <title>

  6. Chandan Kumar
    Member
    Posted 2 years ago #

    To display without title tag write it as:

    echo $blogname;
  7. rhonorginal
    Member
    Posted 2 years ago #

    ?

  8. Chandan Kumar
    Member
    Posted 2 years ago #

    Do you want to wrap your page title with <title> tags or not?

  9. cinghaman
    Member
    Posted 2 years ago #

    try using all in one SEO plugin

  10. rhonorginal
    Member
    Posted 2 years ago #

    I'm thinking something more like

    <title><?php
    	global $page, $paged;
    	wp_title( '|', true, 'right' );
    	bloginfo( 'name' );
    	$site_description = get_bloginfo( 'description', 'display' );
    	if ( $site_description && ( is_home() || is_front_page() ) )
    		echo " | $site_description";
    	if ( $paged >= 2 || $page >= 2 )
    		echo ' | ' . sprintf( __( 'Page %s' ), max( $paged, $page ) );
    ?></title>
  11. rhonorginal
    Member
    Posted 2 years ago #

    yes, solved my own problem again....

    sorry chandanonline4u, your code doesn't work
    use a plugin... what are you on?

  12. cinghaman
    Member
    Posted 2 years ago #

    <title>
    
    <?php if (is_home()) {
    
    echo ' bloginfo( 'name' );'
    }
    else { 
    
    }
    
    ?>
    
    </title>
  13. Chandan Kumar
    Member
    Posted 2 years ago #

    Great!

Topic Closed

This topic has been closed to new replies.

About this Topic