Wordpress custom field if else for showing data - wordpress

In my theme I have custom field 'ext_url'. Now I want to get post url If I don't add 'ext_url' custom field.
I tried
<?php if(get_post_meta($post->ID, 'ext_url', true)): ?>
<?php else : ?>
<?php the_permalink(); ?>
<?php endif; ?>
When I don't add custom field, it is showing post URL. But, when I add custom field it is showing blank. Any Solutions?
Update: I've found a code
<?php
$url = get_post_meta($post->ID, 'ext_url', true);
if ($url) {
echo "<p><a href='$url'>External URL</a></p>";
}
?>
but, it is working for showing custom field. How can i add else function here? Sorry, I am not experienced in PHP.

If you want it to display the data from the custom field, you should add a line to echo the results.
<?php
$ext_url = get_post_meta( $post->ID, 'ext_url', true );
if ( $ext_url ) {
echo $ext_url;
} else {
the_permalink();
}
?>

Related

Wordpress: function to replace content for custom post status

I created custom post status "blocked" via register_post_status (like this register_post_status not showing post_status in status dropdown).
Now I want to replace content on posts with this status via functions.php.
I tried something like this
function block_by_status ($post_object) {
global $wp_query;
if( 'blocked' == $wp_query->post->post_status ) {
return 'This post is blocked';
}
}
add_action('the_post','block_by_status');
But it doesn't work, please somebody help?
So for the content you can do something as simple as:
add_filter('the_content', 'block_by_status');
and to hide the title you can use:
add_filter( 'the_title', 'block_by_status', 10, 2 );
If you want to go further than that, I suggest editing the template in the theme folder.
For pages you need to edit page.php and for posts you need to edit posts.php or singular.php, depends on the theme you are using.
So you can do something like this:
In the functions.php file:
function block_by_status ($post_object) {
global $wp_query;
if( 'blocked' == $wp_query->post->post_status ) {
return true;
}
}
and then in the files I referenced above (Depends on your theme):
<?php if (!block_by_status()) { ?>
<?php while (have_posts()) : the_post(); ?>
<h1><?php the_title() ?></h1>
<?php the_content() ?>
<?php get_sidebar() ?>
<?php endwhile; ?>
<?php } else {
echo 'This post is blocked';
} ?>
So you use your function as a boolean and just hide what you dont want users to see.

How to pass PHP template through shortcode

I want to create an overview of my latest blog posts and display this information on a certain part of my homepage. My WordPress theme already has a PHP template in the desired format, which means I would not have to create the actual design/style myself.
As I want this information to be shown in one particular part, I was thinking about creating a shortcode which inserts the information then. I have created shortcodes in WP before, however they always populated simple information.
I am running into the problem now that I do not quite know how I can populate a PHP template via a shortcdode. Before the shortcode function was like similar to this one:
function display_my_shortcode($atts, $content, $tag){
//if called from the 'my_primary_shortcode' shortcode
if($tag == 'my_primary_shortcode'){
return 'This is the primary shortcode';
}
//if called from the 'my_secondary_shortcode' shortcode
else if($tag == 'my_secondary_shortcode'){
return 'This is the secondary shortcode';
}
//default
else{
return 'This is something else';
}
}
add_shortcode('my_primary_shortcode','display_my_shortcode');
add_shortcode('my_secondary_shortcode','display_my_shortcode');
My problem is, that the output of the template is still with php - the template I want to export would look like this:
<div class="blog_holder blog_small_image">
<?php $counter = 0; ?>
<?php if(have_posts()) : while ( have_posts() ) : the_post(); ?>
<?php
if(get_the_category(get_the_ID()) != 'test123')
{
continue;
}
else
{
if($counter < 6){
get_template_part('blog_small_image', 'loop');
$counter++;
}
}
?>
<?php endwhile; ?>
<?php if($qode_options_theme13['pagination'] != "0") : ?>
<?php pagination($wp_query->max_num_pages, $blog_page_range, $paged); ?>
<?php endif; ?>
<?php else: //If no posts are present ?>
<div class="entry">
<p><?php _e('No posts were found.', 'qode'); ?></p>
</div>
<?php endif; ?>
</div>
It should create a list of 5 blog posts of the category 'test123'. How would I include this output into a shortcode though. what would be the "return" value?
Thank you guys so much!

Wordpress is_page_template query

i use a Plug-in on my wordpress site which uses a query to show search results, thats the code of the query:
if ( $query->have_posts() )
{
?>
<?php echo $query->found_posts; ?> Ergebnisse gefunden<br />
<?php
while ($query->have_posts())
{
$query->the_post();
?>
<div>
<?php if ( has_post_thumbnail() ) { echo '<p>'; the_post_thumbnail("small"); echo '</p>'; } ?>
<h2><?php the_title(); ?></h2>
</div>
<?php
}
?>
Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?><br />
Previous | Next
<?php
}
else
{
echo "No Results Found";
}
The search should only display results from pages who uses a specific page template (mytemplate.php)
I found the Docs http://codex.wordpress.org/Function_Reference/is_page_template who explain this, but i dont get it to work inside the above query.
any help would be nice :) thx
Please do as below.
if ( is_page_template( '{enter the name of page template with extension}' ) ) {
// Enter your if code here
} else {
// Enter your else code here.
}
For this code to work you must select the page template option while creating the page in WordPress Admin Interface.

Load page content and blog post index content (WordPress)

I have a question about Wordpress' template structure and querying posts.
I have my templates setup where the (for example) archive-$posttype.php is built like:
get_header();
$args = 'page_id=18'; //Client Uses page
query_posts($args); the_post();
get_template_part( 'sub-header' );
// Reset Query
wp_reset_query();
?>
<div class="content">
<?php get_template_part( 'loop' ); ?>...
I do this to set my default $post variable for my sub-header.php file which prints out content from that page:
<div id="subheader">
<h1><?php echo get_post_meta($post->ID, 'header_title', true)?></h1>
<?php echo get_post_meta($post->ID, 'header_description', true)?>...
However, using this method on the home.php template, doesn't work:
get_header();
$temp_query = $wp_query;
$page_id = 119; //Client Uses page
#$post = get_page( $page_id );
$args = array('page_id' => $page_id);
$post = new WP_Query( $args ); the_post();
get_template_part( 'sub-header' );
wp_reset_postdata();
?>
<div class="content">
<?php get_template_part( 'loop' ); ?>
<?php get_sidebar( 'news' ); ?>
</div><!--.content -->
<?php get_footer(); ?>
I'm curious why this works on one template and not on the home template. AND, am I going about this the wrong way? What's the correct way to have page content in the sub-header template that in most cases is ACTUALLY related to that current page the user is on.
Thanks!
Not sure I understand your problem exactly but if you are doing what I think, having a chunk of text from a specific page above another loop that pulls in posts of some sort or another I would use the template naming structure.
<?php /* Template Name: My Template */ ?>
This will allow you to use the standard loop to get content from whatever page the clients sets the template to (avoiding the static ids you are using).
Your home page bug could be due to it not being set as the posts page in the reading settings. As I understand it if a page is not set as the posts page it will act like a normal page unless you rewrite the query specifically.
"What's the correct way to have page content in the sub-header template that in most cases is ACTUALLY related to that current page the user is on."
Rather than trying to tie together two unrelated pages/posts, it might be easier to use a custom field tool. My current favorite is Advanced Custom Fields.
With ACF, you can add supplemental fields (image, wysiwyg, file upload, 14 total field types) to your posts and pages, then easily pull the custom data into your template. It's very well documented and extremely easy to use.
So, I changed the way my sub-header.php template works. Basically some basic checking on what type of page/post was being set/called then dynamically pulled the relevant page info.
<?php
if (is_page()) :
$header_title = get_post_meta($post->ID, 'header_title', true);
$video_id = get_post_meta($post->ID, 'youtube_video_id', true);
$thumbnail = get_the_post_thumbnail($post->ID, 'post-thumbnail', array('class'=>'visual-element'));
$description = get_post_meta($post->ID, 'header_description', true);
elseif (is_home()) :
$page_id = 119; // News page
$page = get_page( $page_id );
$header_title = get_post_meta($page->ID, 'header_title', true);
$video_id = get_post_meta($page->ID, 'youtube_video_id', true);
$thumbnail = get_the_post_thumbnail($page->ID, 'post-thumbnail', array('class'=>'visual-element'));
$description = get_post_meta($page->ID, 'header_description', true);
elseif (is_archive()) :
$page_id = 18; // Client Uses page
$page = get_page( $page_id );
$header_title = get_post_meta($page->ID, 'header_title', true);
$video_id = get_post_meta($page->ID, 'youtube_video_id', true);
$thumbnail = get_the_post_thumbnail($page->ID, 'post-thumbnail', array('class'=>'visual-element'));
$description = get_post_meta($page->ID, 'header_description', true);
endif;
?>
<div id="subheader">
<h1><?php echo $header_title; ?></h1>
<?php if ( $video_id ) : ?>
<iframe class="visual-element" width="300" height="200" src="http://www.youtube.com/embed/<?php echo $video_id;?>?rel=0" frameborder="0" allowfullscreen></iframe>
<?php elseif ($thumbnail) : ?>
<?php echo $thumbnail; ?>
<?php endif; ?>
<?php echo $description; ?>
</div><!-- #subheader -->

How can i have custom fields on the posts page in wordpress?

First I've created a home.php page to replace index.php and can add some custom fields on this new one and to have in it lastest 3 posts.
On home.php page I put:
<?php echo get_post_meta($post->ID, 'test', true); ?>"/>
but it doesn't works cause it tries get the post id and not the id of the page. If i put 18 (id of the page) directly it works, but i want it dinamicaly:
<?php echo get_post_meta(18, 'test', true); ?>"/>
And this condition is not satisfied for test:
if($post->post_type == 'page'){
echo 'This item is a page and the ID is: '.$post->ID;
}
Heyo,
I think your problem might be that you should have that within a loop.
For example if you were displaying several post from a category and displaying each custom field it might look like this:
<?php query_posts('cat=25&showposts=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li class="homeblock" style="max-width:400px;">
<span class="artist-name">
<?php $key='artist'; echo get_post_meta($post->ID, $key, true); ?>
</span>
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
</li>
<?php endwhile; endif; ?>
Otherwise (not sure how your theme is set up), what you might have to do is edit content.php and add:
<?php $key='artist'; echo get_post_meta($post->ID, $key, true); ?>
Try:
if ($post->post_type == 'page') {
<?php echo get_post_meta($page_id, 'test', true); ?>
}
Hope this helps :)

Resources