If CPT 'coins' has content else "" (wordpress cptui) - wordpress

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";
}

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();
}
?>

How to get parent page id and title of current page in wordpress?

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 . ' - ';
}
}

How to load different Bloginfo value for top level pages?

I am no PHP programmer therefore I have been scratching my head for two hours on this issue as there seems to be many ways of doing it. None of which I managed to get working because of my poor PHP/Wordpress syntax and logic knowledge.
What is the best way to create and call a function that loads a different value for <?php bloginfo('name') ?> only on top level pages? i.e. top level menu headings.
I was wondering if there would be the possibility of doing something along these lines:
<?php
if ( is_page( 'about' ) || == $post->post_parent ) {
bloginfo = 'about';
} elseif ( is_page( 'work' ) || == $post->post_parent ) {
bloginfo = 'work';
} elseif ( is_page( 'contact' ) || == $post->post_parent ) {
bloginfo = 'contact';
} else {
bloginfo('name');
}
?>
Please treat it as pseudo code as I am still familiarizing myself with the wordpress and PHP codex/syntax, hence why I cannot get anything to work. But basically, what I need to do is use the main nav link names as the bloginfo name for every page in their section. Anything else is an exception which defaults back to the home bloginfo name.
Would it be easier to try to parse the nav link titles themselves into the function?
Please help if you can!
UPDATE
I have completely ditched the previous option and went the following route:
<?php if (is_page() || is_single( array( 62, 57, 51, 8 ) )) {
echo wp_title('');
}
else{
$category = get_the_category();
echo $category[0]->cat_name;
}?>
It is kind of botched but after reading lots of reference material on the wordpress site, I think I am beginning to understand the syntax. So, instead of targeting bloginfo() I am now targeting the page title. However I do not want the posts to have a huge title therefore I am making all pages other than top level "pages" pick up the first category instead, except for a handful of posts which are in fact custom pages, so need to display the wp_title value.
If anyone has any tips on turning this into a more flexible/efficient function please drop a post otherwise I will make this as resolved in a couple of hours.
the syntax would be
<?php
if ( is_page( 'about' ) || == $post->post_parent ) {
bloginfo('about');
} elseif ( is_page( 'work' ) || == $post->post_parent ) {
bloginfo('work');
} elseif ( is_page( 'contact' ) || == $post->post_parent ) {
bloginfo('contact');
} else {
bloginfo('name');
}
?>
but there are no such parameters like about,work,contact to the bloginfo function in wordpress.
For more information on bloginfo, please check the this url -> http://codex.wordpress.org/Function_Reference/bloginfo
You may try this to filter/change the name using bloginfo filter (just add this code snippet in your functions.php file)
function my_custom_blogname( $output, $show )
{
if( $show == 'name' )
{
if( is_page('about') ) $output = 'It\'s my about page';
if( is_page('work') ) $output = 'It\'s my work page';
if( is_page('contact') ) $output = 'It\'s my contact page';
}
return $output;
}
add_filter('bloginfo', 'my_custom_blogname', 10, 2);
Update : Also you can check if the page is parent/top lavel page using
if( !$post->post_parent ) // To check only if it's a parent/top lavel page
if( is_page('about') && !$post->post_parent ) $output = 'about'; // To check if it's a parent/top lavel page and about page

Changing the title of Wordpress Tag Pages

I wish to change the title of Tags pages of my wordpress blog. What I want to do is to keep the Tag name as it's title only.
For example: If a tag name is "Wordpress Tag Pages Tutorial" then that tag should have the same title "Wordpress Tag Pages Tutorial" instead of "Blog Name - Wordpress Tag Pages Tutorial" so what to change in this code? I have tried but showing errors like only variables name in wordpress title.
<title>
<?php if (is_home () ) { bloginfo('name'); }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); } ?>
You're missing is_tag().
Example:
if ( is_tag() ) {
$tag = single_tag_title("", false);
echo $tag;
}
If there are any SEO plugins installed in the site, it will override the 'wp_title' function. So you can manage the title string in that plugin settings.
If there are no such plugins, you can use following code:
add_filter('wp_title', 'hs_tag_titles',10);
function hs_tag_titles($orig_title) {
global $post;
$tag_title = 'Test Title';
if ( is_tag() ) {
return $tag_title;
} else {
return $orig_title;
}
}

How to display blog category as title in Voltage Wordpress theme?

My client has a site that was built using Themeforest's Voltage Wordpress theme. I have all
of the tweaking they want done except for one thing.
I have created 3 blog categories, with each one being a nav bar item. When I click any of them,
the page that comes has the correct posts on it, but the header on all three pages reads
"Blog Archive".
See here: http://s11.mynewsitereview.com/
The three categories are Fashion & Culture, My Style, and Hints & Tips.
Here is the code from index.php that generates the page title:
<h1><?php
if(single_cat_title("", false)=='' && !is_archive()) {
$page = get_page_by_title( single_post_title('', false) );
$vltg_page_meta=get_post_meta($page->ID, '_vltg_page_meta', true);
if(isset($vltg_page_meta['display_title']) && $vltg_page_meta['display_title']!='') {
echo $vltg_page_meta['display_title'];
} else {
single_post_title();
}
} else if(is_archive()) {
if ( have_posts() ) {
the_post();
if(is_day()) {
echo get_the_date();
} else if(is_month()) {
echo get_the_date('F Y');
} else if(is_year()) {
echo get_the_date('Y');
} else {
echo 'Blog Archives';
}
} else {
echo 'Blog Archives';
}
rewind_posts();
} else {
single_cat_title();
} ?></h1>
I can see why it is generating "Blog Archives" as the title, but I don't know how to tweak it
so that it will display the category name instead.
Any help would be most appreciated!
:)
OK, I finally solved this one after getting no love from the developer of the Voltage theme.
Have a look at this part of the code I posted in my original question:
} else {
echo 'Blog Archives';
}
} else {
echo 'Blog Archives';
}
Here is what I changed it to:
} else {
single_cat_title();
}
} else {
single_cat_title();
}
And it works!

Resources