Community Forums › Forums › Archived Forums › Design Tips and Tricks › I can't filter nav menu items from this code
Tagged: genesis_entry_header, ion icons, is_singular
- This topic has 8 replies, 3 voices, and was last updated 7 years, 11 months ago by
Erika.
-
AuthorPosts
-
April 11, 2017 at 5:37 pm #204643
Erika
ParticipantI'm trying to display an icon above my post titles, and the code below works, except that the icon also appears beside my nav menu links:
//* Image above Title
function new_title( $title ) {
$new_title = '<i class="ion-fork"></i><br> ' . $title;
return $new_title;
}
add_filter( 'the_title', 'new_title' );How do I filter out the nav menu items? I've tried a dozen different things and nothing works!
http://demo.risenuponthee.com/ultimate-chef/2017/04/11/sample-post-with-headings/April 11, 2017 at 5:43 pm #204644carasmo
ParticipantYou would add a conditional that says: it's not admin and in the loop and a single page
function new_title( $title ) { if( ! is_admin() && in_the_loop() && is_singular() ) { $new_title = '<i class="ion-fork">x</i><br> ' . $title; return $new_title; }//end conditional return $title; } add_filter( 'the_title', 'new_title' );
April 11, 2017 at 5:44 pm #204645carasmo
Participantif you want both archive and single, just remove the
&& is_singluar()
April 11, 2017 at 6:13 pm #204647Erika
ParticipantCarasmo,
Thank you so much! This worked perfectly!
One last question: How do I filter out page titles?
April 11, 2017 at 9:14 pm #204648carasmo
ParticipantHow do you skip specific page titles? I would make a helper like this using the page/post id:
/** * * Is Page Title Helper * @since 1.0.0 * */ function is_not_this_page() { return ( ( is_page( array( '414', '530', '1107', '1589', '1638', '1134', '369' // last has no comma ) ) ) ) ? true : false ; }
Then apply it with the ! not conditional like this which says:
is not admin and in the loop and is singular and not any of those pages in the array:
function new_title( $title ) { if( ! is_admin() && in_the_loop() && is_singular() && ! is_not_this_page() ) { $new_title = '<i class="ion-fork">x</i><br> ' . $title; return $new_title; }//end conditional return $title; } add_filter( 'the_title', 'new_title' );
April 11, 2017 at 9:19 pm #204649carasmo
ParticipantThose are for pages, if this is a mixture of posts and pages use is_single instead in the is_not_this_page helper. Please read https://codex.wordpress.org/Conditional_Tags then you can see how to get what you want for your needs.
April 11, 2017 at 9:35 pm #204651carasmo
ParticipantOops, that is wrong. is_single is just posts and cpts but not pages.
function is_not_this_page() { return ( ( //pages I don't want is_page( array( '414', '530', '1107', '1134', '369' // last has no comma ) ) || // or //posts and cpts I don't want is_single( array( '123', '245', '789' // last has no comma ) ) ) ) ? true : false ; }
April 11, 2017 at 10:16 pm #204653Brad Dalton
ParticipantHere's another method:
add_action( 'wp_enqueue_scripts', 'bg_enqueue_ionicons' ); function bg_enqueue_ionicons() { wp_enqueue_style( 'ionicons', '//code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css', array(), CHILD_THEME_VERSION ); } add_action( 'genesis_entry_header', 'icon_before_post_title', 5 ); function icon_before_post_title() { if ( is_singular( 'post' ) ) { echo '<h1><i class="ion-fork entry-title"></i></h1>'; } }
April 12, 2017 at 2:22 pm #204696Erika
ParticipantThank you both! Both codes are effective.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.