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();
}
?>
Related
Im looking for a solution where I use an if else statement.. So the if statement must check if there is content in de CPT else give html. I had something but that doesnt work.
I use WP (latest version), CPT UI (latest version).
CPT = Coins
If cptui coins has content {
CPT UI = Coins then show content / description
} else {
give html
}
Regards Dutchy
Ok find it out myself did it like so and now it does work perfectly:
<?php
$mypost = get_post();
echo apply_filters('the_content',$mypost->post_content);
if ( !empty( $mypost->post_content ) ) {
the_content();
} else {
echo '<p ng-bind-html="ctrl.Coin.descriptionhtml"></p>';
}
?>
You can use this if it's on single-coins.php
if ( !empty( get_the_content() ) ) {
the_content();
} else {
echo "HTML PART";
}
Here is the my code, which is not working.
global $post;
echo get_the_title( wp_get_post_parent_id( $post->post->ID ) );
but this is not working.
thank you in advance.
For parent page id
$post->post_parent;
For current page title
$post->post_title;
For parent page id
<?php
echo wp_get_post_parent_id(get_the_ID());
?>
In Gutenberg:
wp.data.select('core/editor').getEditedPostAttribute('parent')
Hope will be helpful to someone
If you want i.e: create a link to the post parent:
<a href="<?= get_permalink($post->post_parent) ?>">
<?= get_the_title($post->post_parent) ?>
</a>
→ <?= the_title() ?>
which will result in i.e:
Latest News → Some news title
For Astra theme and for page template look.php I did this:
$post->post_parent; will nbot work cause in my case function is out of the loop. I run it via functions.php. $post->post_parent works perfectly when inserting it in page template, but not when editing theme function ;)
function add_script_before_header() {
$current = $post->ID;
$parent = $post->post_parent;
$grandparent_get = get_post($parent);
$grandparent = $grandparent_get->post_parent;
if ($root_parent = get_the_title($grandparent) !== $root_parent = get_the_title($current)) {
echo get_the_title($grandparent);
}
$after = $parent;
if ( is_page_template( 'look.php' ) ) {
echo $after . ' - ';
}
}
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>
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();
I am using this code to get the tags in my wordpress posts for a theme
`<?php
$posttags = get_the_tags();
if ($posttags) {
foreach ($posttags as $tag) {
$tagnames[count($tagnames)] = $tag->name;
}
$comma_separated_tagnames = implode(", ", $tagnames);
print_r($comma_separated_tagnames);
}
?>`
The PROBLEM is that it is returning tags for "all posts" not just individual posts, and I think the problem is that if a post DOESNT have tags - it just inserts tags anyway.
Can anyone help modify this so:
It return tags only for a post - not all tags
If there are no tags for a post, dont return anything
P.S - Can check out here for the wordpress docs
<footer class="entry-footer">
<?php //get all tags for the post
$t = wp_get_post_tags($post->ID);
echo "<p class='tags-list'>TAGGED WITH: ";
foreach ($t as $tag) {
$tag_link = get_tag_link($tag->term_id);
echo "<a href='$tag_link' class='used-tag' rel='tag'>".($tag->name)."</a> ";
}
echo "</p>";
?>
</footer>
This is what I did, displays tags for each post in the loop.
Try using:
<?php the_tags(); ?>
Inside the 'Loop'.
Function Reference
alright I hope this could help someone , I stuck on this issue for about hour
trying to get tags of my post "so I can mix it with twitter share link "
the_tags(); function were useless since I use it out of WP loop , get_the_tag_list(); were perfect to me since it can include post id ,
$postid = $wp_query->post->ID;
$posttags = strip_tags( get_the_tag_list( ' ', '', '', "$postid" ) ) ;
^^ the above code I stripped html codes to get tag name without href link .
this is the function use case :-
get_the_tag_list( string $before = '', string $sep = '', string $after = '', int $id )