I have the following problem. I am using Advanced Custom Fields for Wordpress to create a subtitle field for a post. I like to give this subtitle some styling but my HTML code within the IF-statement doesn't show on the page. The $subtitle does show.
<?php $subtitle = the_field('subtitle'); ?>
<?php if(strlen(trim($subtitle)) > 0): ?>
<div class="post-sub-title"><?php $subtitle; ?></div>
<?php endif; ?>
I spent hours searching for similar problems but couldn't find any solutions. So this must probably be a rookie mistake on my part.
Solution
<?php $subtitle = get_field('subtitle'); ?>
<?php if(strlen(trim($subtitle)) > 0): ?>
<div class="post-sub-title"><?php $subtitle; ?></div>
<?php endif; ?>
Changed the_field() to get_field(). Kuddo's to Aditya Vikas!
You should echo/print it (The subtitle variable).
<div class="post-sub-title"><?php echo $subtitle; ?></div>
try using this piece of code :
<?php if(strlen(trim($subtitle)) > 0): ?>
<div class="post-sub-title"><?=$subtitle?></div>
<?php endif; ?>
instead of this :
<?php if(strlen(trim($subtitle)) > 0): ?>
<div class="post-sub-title"><?php $subtitle; ?></div>
<?php endif; ?>
and also one more thing !
the_field() is not a default WordPress function
the 'whatever' plugin you are using might have a corresponding function:
get_field()
try this one:
<?php $subtitle = get_field('subtitle'); ?>
<?php if(!empty(trim($subtitle))): ?>
<div class="post-sub-title"><?php $subtitle; ?></div>
<?php endif; ?>
Thanks.
Related
I have a code chunk that is inside the_content(); I'm using acf repeater as well. So when I post a blog, I'll either use the_content(); or the acf field. I have h2 tag ( latest articles ) that I only want printed one time, but it's printing everytime I make a post.
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="container">
<div class="row">
<div class="col-md-4 sidebar-r">
<?php echo the_content(); ?>
</div><!-- end sidebar-r -->
<?php
$i = $wp_query->post_count;
if($i <=1) {
echo '<h2 class="link-title">
<?php the_sub_field('link_title'); ?>,
</h2>';
}else{
echo '';
}
?>
<div class="col-md-8 links-wrap">
<?php if(have_rows('daily_links')): ?>
<?php while(have_rows('daily_links')): the_row(); ?>
<a href="<?php the_sub_field('link_url'); ?>" target="_blank">
<h2 class="link-title">
<?php the_sub_field('link_title'); ?>,
</h2>
<h3 class="link-source">
<?php the_sub_field('link_source'); ?>
</h3>
</a>
<?php endwhile; ?>
<?php endif; ?>
</div><!-- end links wrap -->
</div><!-- end row -->
</div><!-- end container -->
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
You'll see I tried using php to count the posts and if more than one post, don't print the tag, but couldn't figure out the exact logic and syntax.
I am honestly struggling a bit to understand exactly what you are trying to do and since I do not even have the posts and other key pieces of information so that I can properly replicate your issue so that I can help you better, this is a little bit challenging. That being said, looking into some ideas I came across another stackoverflow question/answer that might be relevant for you in catching the first post and does something to it. The answer to the referenced question instance was this:
<?php if (have_posts()) : $postCount = 1; while (have_posts()) : $postCount++; ?>
<?php if($postCount == 2) { ?>
// SOMETHING TO DO WITH FIRST POST
<?php } else { ?>
// SOMETHING TO DO WITH ALL OTHER POSTS
<?php } ?>
This was suggested by user Bora in this answer from 2013.
Let me know if that helped!
hi i has a wordpress blog i put this code below within single.php page in side bar
the code is the same code in the hompage side bar >
this code is correctly work in hompage sidebar and return to all posts
but in the single.php sidebar only return to one post.
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="new-posts-form">
<div class="new-posts-img"><?php the_post_thumbnail(array(100,80)); ?></div>
<div class="new-posts-title"><?php the_title(); ?></div>
<div class="new-post-description"><?php content('11'); ?></div>
<div class="new-post-add-date-time"><img src="<?php bloginfo('template_url'); ?>/images/tb_clock.png" /><?php the_time('F j, Y'); ?> at <?php the_time('g:i a'); ?> : <?php the_category(', ') ?></div>
<?php endwhile; ?>
<?php endif; ?>
I think on single.php page you should use this
for reference please check
http://codex.wordpress.org/Function_Reference/get_post
i am trying to solve your problem hope this help.
thanks
anand
I'm currently trying to develop a custom Wordpress theme, and on my homepage I need to add a second content block. I am using a plugin to do this, which simply requires me to add the following where I want the content block to be.
<?php the_block('Latest Products')?>
However when I add this it seems to have no effect which I believe is due to the formatting of my php. I'm fairly new to php, so any help is greatly appreciated.
My code is as follows - I've cut out the best part of the HTML. I think it's something to do with that 'endforeach' tag?
<?php get_header(); ?>
<?php if(have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php
global $post;
$myposts = get_posts('numberposts=4&category=1');
foreach($myposts as $post) :
?>
<div class="blogsnippet">
<div class="postdate">
<span class="top"><?php the_time ('j')?></span><br/><span class="bottom"><?php the_time('M');?></span>
</div>
<div class="postexcerpt">
<h3><?php the_title(); ?></h3>
<p><?php echo(get_the_excerpt());?></p>
</div>
</div>
<?php endforeach;?>
<?php the_block('Latest Products')?>
<?php endwhile; endif; ?>
<?php get_footer(); ?>
EDIT
Okay, so apparently it needs to be put outside the loop, however it still won't work. Any ideas?
<?php get_header(); ?>
<?php if(have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php
global $post;
$myposts = get_posts('numberposts=4&category=1');
foreach($myposts as $post) :
?>
<div class="blogsnippet">
<div class="postdate">
<span class="top"><?php the_time ('j')?></span><br/><span class="bottom"><?php the_time('M');?></span>
</div>
<div class="postexcerpt">
<h3><?php the_title(); ?></h3>
<p><?php echo(get_the_excerpt());?></p>
</div>
</div>
<?php endforeach;?>
<?php endwhile; endif; ?>
<?php the_block('Latest Products')?>
<?php get_footer(); ?>
This mostly depends on what the plugin is actually doing because your code syntax is correct.
If you are using the Multiple Content Blocks plugin and are using the latest Wordpress version 3.5.1 then I believe the plugin may not be compatible. I'd check the version compatibility of the plugin to your Wordpress install as this could be your issue.
EDIT:
The plugin works by applying a filter to the function the_content() so that is why it only works by declaring the_block() before the_content() function is called.
A solution could be to capture the output the_block() and use print it out later, as an example:
<?php
ob_start();
the_block('Latest Products');
$latest_products_contents = ob_get_contents();
ob_end_clean();
?>
<!-- Further down.. -->
<?php echo $latest_products_contents; ?>
I'm new to Wordpress and PHP so not sure how to word this problem.
I'm developing a Wordpress blog with custom pages (2 column layout for the pages) and I'm using Advanced Custom Fields.
Anyways so I have a custom field called content_row which has a sub_field named row and that has 2 sub_fields named left_column and right_column.
For some reason I'm not able to pull the row content :(
My page.php code (on the first echo I'm getting a "bool(false)" on the screen):
<?php get_header(); ?>
<section style="margin-top:400px;">
<?php
if( get_field('content_row') ): ?>
<?php echo var_dump(has_sub_field('row')); ?>
<?php while( has_sub_field('row') ): ?>
<?php echo "test"; ?>
<?php endwhile; ?>
<?php endif; ?>
</section>
<?php get_footer(); ?>
Any ideas / thoughts? Let me know if you guys need to see more code... some screenshots below:
Got it figured out!
<?php while(the_flexible_field("row")): ?>
<?php if(get_row_layout() == "2_column"): // layout: Content ?>
<div class="content">
<?php the_sub_field("left_column"); ?>
<?php the_sub_field("right_column"); ?>
</div>
</div>
<?php endif; ?>
Question about a piece of PHP code in a WordPress template file.
The template contains this code:
<h1><?php the_title(); ?></h1>
I want the title only be printed if the title is not "Home".
But this code doesn't work:
<?php if (the_title()!='Home'); ?>
<h1><?php the_title(); ?></h1>
<?php endif; ?>
the_title() echoes, it doesn't return its title.
Use get_the_title() instead.
<?php if (get_the_title() != 'Home'): ?>
<h1><?php the_title(); ?></h1>
<?php endif; ?>
As an aside, it looks like you're trying to detect if you're on the home page. Checking against a title can be flaky, as that can change.
Use is_home() instead.
<?php if ( ! is_home()): ?>
<h1><?php the_title(); ?></h1>
<?php endif; ?>
<?php if (the_title()!='Home'): ?>
^
Use : instead of ;
link
Or you can use
http://codex.wordpress.org/Function_Reference/is_front_page
Another simple solution:
<?php if (the_title()!='Home') { ?>
<h1><?php the_title(); ?></h1>
<?php } ?>