Wy am I getting fatal error message with this code - wordpress

I am using the genesis sample child theme and I getting the following error message: Fatal error: Cannot redeclare landing_do_content() (previously declared in XXX. I have reviewed all files and the function is not declared anywhere else.
`// Display content of flexible content layouts
add_action( 'genesis_entry_content', 'landing_acf_content' );
function landing_acf_content() {
if( have_rows('landing_page_content') ) { ?>
// Hero with text and button layout
if( get_row_layout() == 'hero_with_text_and_button' ) { ?>
<div class="landing-hero" style="background: url(<?php echo get_sub_field('hero_image'); ?>); background-size: cover;">
<div class="hero-content light dark">
<div class="wrap">
<h1 class="hero-title"><?php echo get_sub_field('headline'); ?></h1>
<p><?php echo get_sub_field('text'); ?></p>
<a class="button" href="<?php echo get_sub_field('url'); ?>"><?php echo get_sub_field('button'); ?></a>
</div>
</div>
</div>
<?php }
// Headline with WYSIWYG
else if( get_row_layout() == 'headline_with_wysiwyg' ) { ?>
<div class="heading-text" style="background-color: <?php echo get_sub_field('background_color'); ?>">
<div class="wrap">
<h2 class="plain-title"><?php echo get_sub_field('headline'); ?></h2>
<?php echo get_sub_field('content'); ?>
</div>
</div>
<?php }
<?php }
}`

If you are declaring a global function and not one inside a class, make sure to only call the file with this function only once, if you require or include the php file more than once, it will result in redeclaration error.

Related

How to get Co-Author-Plus data via shortcode

with the following code I want to show the authors via the Co-Author-Plus plugin via shortcode using a php file. But this doesnt work. It just returns nothing. You know why?
Update code:
<?php function coauthorplus_function() {
ob_start();
$coauthors = get_coauthors();
$len = count( $coauthors );
if ( (function_exists( 'coauthors_posts_links' )) && ($len > 1) ) { ?>
<div class="authors-container">
<div class="author-image">
<?php echo get_avatar( $coauthor->user_email, '80'); ?>
</div>
<div class="author-data-details">
<div class="fdescription fdescriptions">Dieser Artikel wurde in Ko-Produktion durch folgende Autoren erstellt:</div>
<div class="authors-name">Von <?php coauthors_posts_links(); ?></div>
</div>
<div class="clearfix"></div>
</div>
<?php
} else {
global $post;
$author_id=$post->post_author;
foreach( get_coauthors() as $coauthor ):
?>
<div class="author-container">
<div class="author-image">
<?php echo get_avatar( $coauthor->user_email, '80'); ?>
</div>
<div class="author-data-details">
<div class="author-name">Von <?php coauthors_posts_links(); ?></div>
<div class="fdescription"> <?php echo $coauthor->description; ?></div>
</div>
<div class="clearfix"></div>
</div>
<?php endforeach;
}
return ob_get_clean(); } add_shortcode('coauthorplus', 'coauthorplus_function'); ?>
In my function.php which is on the same place in the webspace I included the following:
include('coauthorplus.php');

How to override a function in parent WordPress theme

I am building a child theme using Flaxseed as a parent. This theme includes several 'content styles' which can be selected using Theme > Customizer. Each of these styles has a function in the parent functions.php file that renders the CSS and wordpress content into one. I want to edit some of the Loop functions that are in here, specifically updating the_excerpt to the_content, but I want to do so in a way that works with my child theme.
Since this code is in the parent functions.php, and since the theme customizer calls it specifically by name, I cannot simply add a new function, and I cannot override it using the same function name.
My best guess is that I need to somehow remove this function from the parent functions.php before loading a new function of the same name in my child function.php, but I cannot seem to figure out how.
Below is the code from the template file where the function is loaded into the template:
<?php for ($i = 1; $i < 8; $i++)
if (get_theme_mod('flaxseedpro_fa_'.$i) != 'none') {
flaxseedpro_display_featured_area(
get_theme_mod('flaxseedpro_fa_'.$i),
get_theme_mod('flaxseedpro_fa_title'.$i),
get_theme_mod('flaxseedpro_fa_cat'.$i)
);
}
?>
Here, the $i variable is the value that is set in the Theme > Customizer screen. This file and code could be modified easily as a part of the child theme.
Below are two code snips from the parent functions.php which select the appropriate featured area code:
function flaxseedpro_display_featured_area($style, $fa_title, $cat) {
if (is_home()) :
switch ($style) {
case 'carousel':
flaxseedpro_carousel($fa_title, $cat);
break;
case 'style1':
flaxseedpro_featured_style1($fa_title, $cat);
break;
case 'style2':
flaxseedpro_featured_style2($fa_title, $cat);
break;
case 'style3':
flaxseedpro_featured_style3($fa_title, $cat);
break;
case 'style4':
flaxseedpro_featured_style4($fa_title, $cat);
break;
default:
break;
}
endif;
}
which leads to several functions such as this:
function flaxseedpro_featured_style2($fa_title, $cat) {
?>
<div class="featured-style1 featured-style2">
<?php if ('' !== $fa_title) : ?>
<h2 class="featured-section-title container">
<?php echo esc_html($fa_title); ?>
</h2>
<?php endif; ?>
<div class="featured-posts-inner container">
<?php
$args = array(
'posts_per_page' => 5,
'cat' => $cat,
);
$fa_posts = new WP_Query($args);
if ($fa_posts->have_posts()) :
$counter = 0;
while ($fa_posts->have_posts()) :
$fa_posts->the_post();
$counter++;
if (1 === $counter) {
?>
<div class="feat-col1 md-6 sm-6">
<div class="md-12 pr-0 pl-0">
<article id="post-<?php the_ID(); ?>" <?php post_class('featured-post-item'); ?>>
<div class="item-container mb-3">
<?php if (has_post_thumbnail()) : ?>
<?php the_post_thumbnail('medium-large', array('class' => 'featured-post-thumbnail-primary')); ?>
<?php else : ?>
<img src="<?php echo esc_url(get_template_directory_uri() . '/assets/images/placeholder.png'); ?>">
<?php endif; ?>
</div>
<div class="post-title-parent">
<a class="post-title title-font" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<div class="post-author">
<?php esc_html_e('By', 'flaxseed-pro'); ?> <?php the_author(); ?> <i class="fa fa-clock-o"></i> <?php the_time('F j, Y'); ?>
</div>
<div class="entry-excerpt body-font mb-3"><?php the_excerpt(); ?></div>
</div>
<?php _e('Read More','flaxseed-pro') ?>
</article>
</div>
</div>
<?php
} else {
?>
<div class="feat-col2 md-6 sm-6">
<article id="post-<?php the_ID(); ?>" <?php post_class('featured-post-item'); ?>>
<div class="md-4 xs-4">
<div class="item-container">
<?php if (has_post_thumbnail()) : ?>
<?php the_post_thumbnail('medium', array('class' => 'featured-post-thumbnail-secondary')); ?>
<?php else : ?>
<img src="<?php echo esc_url(get_template_directory_uri() . '/assets/images/placeholder.png'); ?>">
<?php endif; ?>
</div>
</div>
<div class="md-8 xs-8">
<div class="post-title-parent">
<a class="post-title title-font" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<br><small><?php esc_html_e('By', 'flaxseed-pro'); ?> <?php the_author(); ?> <i class="fa fa-clock-o"></i> <?php the_time('F j, Y'); ?></small>
</div>
</div>
</article>
</div>
<?php
}
endwhile;
endif;
wp_reset_postdata();
?>
</div>
</div>
<?php
}
This is the the code I've been trying to override but unable to find a solution as a part of the child theme. All of the answers I've found seem to require a hook, which I cannot discern from this code.
This is solved.
I changed the function name that is called in my template file (flaxseedpro_display_featured_area >> flaxseedpro_display_featured_area2), then redefined the switch function to replace 'style3' with a new 'style5'. Then added the new 'style5' function with updated CSS code.

ACF Odd/Even Alternate Layout with Relationship

I'm trying to create a two column element using ACF repeater fields. The only thing is I'm trying to alternate this so that the content in the two columns switches left/right depending on the row being even or odd.
The output will look something like:
This is how I looped:
<section id="projects" class="container specific-margin-1">
<!-- Repeater -->
<div class="row">
<?php $i=0 ; if(get_field( 'featured_projects')): ?>
<?php while(has_sub_field( 'featured_projects')): $i++; ?>
<div class="row">
<?php
/*
* Loop through post objects (assuming this is a multi-select field) ( setup postdata )
* Using this method, you can use all the normal WP functions as the $post object is temporarily initialized within the loop
* Read more: http://codex.wordpress.org/Template_Tags/get_posts#Reset_after_Postlists_with_offset
*/
$post_objects = get_field('featured_projects');
if($post_objects ): ?>
<ul>
<?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<div class="repeater_row <?php if (($i % 2)==0 ): ?>col-lg-3 col-md-4 col-sm-6<?php else: ?>col-lg-9 col-md-8 col-sm-6<?php endif; ?>">
<?php if (($i % 2)==0 ): ?>
<li>
<h3><?php the_title(); ?></h3>
<h3><?php the_field('project_location'); ?></h3>
<br>
<span><?php the_field('project_area'); ?></span><br>
<span><?php the_field('project_scale'); ?></span><br>
<br>
<span style="color:#EE1601;">Explore this project <i class="fa fa-arrow-circle-right" aria-hidden="true"></i></span>
</li>
<?php else:
// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<?php endif; ?>
</div>
<div class="repeater_row <?php if (($i % 2)==0 ): ?>col-lg-9 col-md-8 col-sm-6<?php else: ?>col-lg-3 col-md-4 col-sm-6<?php endif; ?>">
<?php if (($i % 2)==0 ): ?>
<?php // check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<?php else: ?>
<li>
<?php // check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<h3><?php the_title(); ?></h3>
<h3><?php the_field('project_location'); ?></h3>
<br>
<span><?php the_field('project_area'); ?></span><br>
<span><?php the_field('project_scale'); ?></span><br>
<br>
<span style="color:#EE1601;">Explore this project <i class="fa fa-arrow-circle-right" aria-hidden="true"></i></span>
</li>
<?php endif; ?>
</div>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif;?>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<!-- Repeater -->
</section>
My Current Output looks like this:
This is the minimal logic for having alternate image and text row wise.
<div class="container">
<div class="row">
<?php
for ($i = 1; $i <= 10; $i++)
{
//even counter will have right image
if ($i % 2 == 0)
{
?>
<div class="col-md-12">
<div class="col-md-4">
Title Number: <?php echo $i; ?>
</div>
<div class="col-md-8">
<img src="" class="img-responsive"/>
</div>
</div>
<?php
}
//odd counter will have left image
else
{
?>
<div class="col-md-12">
<div class="col-md-8">
<img src="" class="img-responsive"/>
</div>
<div class="col-md-4">
Title Number: <?php echo $i; ?>
</div>
</div>
<?php
}
}
?>
</div>
</div>
Hope this will give you an idea.

How to display from category 17 only one post?

I have some code that I was trying to display only one post from the category 17. But I can't it. Anybody have any idea? my code is below. I have tried it but show all post of category 17. I want to show only one post from cat 17.
<?php
if(have_posts()):?>
<?php while(have_posts()):?>
<?php the_post();?>
<?php
$post = $wp_query->post;
if ( in_category( '17' ) ) {
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<div class="car_box shadow left">
<h1><?php the_title(); ?></h1>
<!--<img src="images/car2.png" alt="PREMIUM CARS" align="middle"/>-->
<?php
// Post Image.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
else {
echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/thumbnail-default.jpg" />';
}
?>
<p>BMW 7 Series or simillar</p>
<div class="desc left">
<div class="passenger left">
4
</div>
<div class="suitcase left">
3
</div>
<div class="doors left">
5
</div>
<div class="gear left">
Auto
</div>
</div>
View More
</div>
</a>
<?php } ?>
<?php endwhile;?>
<?php endif;?>
Add break; in the loop after you've shown your first found post, that would be before you close the inner if statement.

the_author is empty when not logged in

Is there a reason why the_author should return an empty string on the first page when the user is not logged in, but return the author name just as it should when more posts are loaded through AJAX ?
The loop is the same in both cases.
Please help me resolve this issue as I'm clueless and I need it fixed ASAP to launch my site.
Here's the whole index.php:
<?php
get_header();
get_sidebar();
?>
<!-- MAIN DIV -->
<div id='content_and_floater'>
<?php get_template_part('social_floater'); ?>
<div id='content'>
<?php get_template_part('loop'); ?>
</div>
</div>
<?php get_template_part('loader'); ?>
<!-- MAIN DIV -->
<?php
get_footer();
?>
And here's how the infinitePaginator calls the loop in functions.php (the function is called when scrolled down to the bottom or the loader link is clicked):
function wp_infinitepaginate(){
$loopFile = $_POST['loop_file'];
$paged = $_POST['page_no'];
$posts_per_page = get_option('posts_per_page');
# Load the posts
query_posts(array('paged' => $paged ));
get_template_part( $loopFile );
exit;
}
You can see the behaviour at test.nowillnoskill.net.
In single posts it's not working either. My guess is that query_posts(array('paged' => $paged )); changed something in the query, but I don't know what is it.
I tried to insert setup_postdata($post); just after the_post() in loop.php as I found that worked for someone, but it doesn't for me.
I also tried to insert
query_posts(array('paged' => 1 ));
before calling the loop file in index.php, but no posts at all were shown.
Here is my loop.php:
<?php while ( have_posts() ) : the_post() ?>
<!-- POST1 -->
<article class='post'>
<header class='post_header'>
<?php
global $current_user;
$current_user = wp_get_current_user();
if (!empty($current_user)) {
$pid = get_the_id();
$uid = $current_user->ID;
$title = (is_favorite($pid, $uid)) ?
'Remove from favorites' :
'Add to favorites';
$trans = (is_favorite($pid, $uid)) ?
'' :
' transparent';
?>
<div>
<h2>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
<?php if (is_user_logged_in()) { ?>
<a title='<?php echo $title ?>' class='post_favorite' href='#' alt='fpid=<?php echo $pid ?>uid=<?php echo $uid ?>'>
<span class='symbol<?php echo $trans ?>'>R</span>
</a>
<?php } ?>
</div>
<div class='post_header_div'>
<strong class='post_category'>
<?php echo get_the_category_list(', '); ?>
</strong>
<strong class='post_author'>
<span class='symbol'>U</span>
<?php the_author(); ?>
</strong>
</div>
<div>
<span class='post_author'>
<?php edit_post_link('[edit]'); ?>
</span>
</div>
<?php } ?>
</header>
<figure class='post_image'>
<!--<img src='design/img/flashkick.png' alt='logo' />-->
<?php the_post_thumbnail(); ?>
</figure>
<div class='post_perex'>
<?php the_content('Read more'); ?>
</div>
<div class='space'></div>
<footer class='post_footer'>
<div class='post_footer_top'>
<div class='post_tags'>
<?php the_tags('', '', ''); ?>
</div>
<div class='post_time'>
<time datetime="<?php the_time('Y-m-d'); ?>" pubdate>
<span class='symbol'>P </span>
<?php relative_post_the_date(); ?>
</time>
</div>
</div>
</footer>
<div class='space'></div>
</article>
<?php endwhile; ?>
Author informations are given by Wordpress in post informations.
Try to do a var_dump on your query_posts result, you should find where the author name is stored, to display it properly.
Can you please show your loop template ? At least the part displaying the author.

Resources