how to define count++ in wordpress php? - php

i built a short-code in wordpress to show related posts in the sidebar. Please look on the right at this website: http://www.immvestwolf.de/news/
But i get a php error (Notice: Undefined variable: count in /web/1/000/045/787/175759/htdocs/immvestwolf/wp-content/themes/le-quartier/functions.php on line 74)
i dont knwo whats wrong with this code. On other sites the code works without an php error.
Here is my php code :
function my_recent_posts_with_image() {
// Lese die letzten zehn publizierten Artikel aus
$args = array(
'posts_per_page' => 10
);
$recent_posts = get_posts( $args );
echo '<div class="widget recent_posts_with_image_by_jongo">';
echo '<h5 class="widget_title">Die 10 letzten News</h5>';
foreach ( $recent_posts as $post ) {
$count++;
?>
<div>
<a title="Veröffentlich am: <?php echo get_the_time('d.m.Y', $post->ID ) ?>" href="<?php echo get_permalink( $post->ID ); ?>"><?php echo get_the_post_thumbnail( $post->ID, array(70,50) ); ?><p><?php echo get_the_title( $post->ID ); ?></p>
</a>
</div>
<?php
}
echo '</div>';
}
add_shortcode('get_recent_posts_with_image','my_recent_posts_with_image');
The error is at the line $count++;
Any ideas to correct this?

$count is never defined...It's up to you to define it. For example:
$count = 0;
foreach ( $recent_posts as $post ) {
That said, you're never even using the value of $count...so it appears as if you could remove it altogether.
Another thing to note is that if you were to use a WP_Query instead of get_posts(), you would have access to a $current_post property for this purpose, without having to manually set up a counter.

Related

Wordpress post_title() notice

here is my scenario: I got an index.php which loads every page I got one below the other. It's a one-pager in the end.
I found this snippet online and It helped me a lot by creating this.
<?php
$mypages = get_pages( array('child_of' => 1503, 'sort_column' => 'menu_order') );
foreach( $mypages as $page ) {
$content = $page->post_content;
if ( ! $content ) // Check for empty page
continue;
$content = apply_filters( 'the_content', $content );
?>
<section>
<div class="headlines info-header"><img src="<?php echo get_template_directory_uri(); ?>/assets/images/arrow.png" alt="arrow"><?php echo $page->post_title; ?></div>
<div class="page-content"><?php echo $content; ?></div>
</section>
<?php
}
?>
It seems to work just fine. But now to the problem part of this. I want to display the posts also on this one page. My try was to create another page news and place a widget on this page which shows the latests posts, filtered by category.
But now the page title won't show up anymore, all I got is the following
notice: NOTICE: TRYING TO GET PROPERTY 'POST_TITLE' OF NON-OBJECT IN
So I thought the problem might have to do with the little <?php echo $page->post_title; ?> I already tried to just get the_titlebut I ended up getting the same error.
I'm kinda new to Wordpress so I don't know how to deal with this.
Is there another way to get the page title on my index.php?
Best,
Dominik
I managed to fix this. As #delboy1978uk told me, I check if the title is not empty by adding 2 lines of code.
Here is the final code:
<?php
$mypages = get_pages( array('child_of' => 1503, 'sort_column' => 'menu_order') );
foreach( $mypages as $page ) {
$content = $page->post_content;
$title = $page->post_title;
if ( ! $content ) // Check for empty page
if ( ! $title) // Check for empty title < NEW!
continue;
$content = apply_filters( 'the_content', $content );
?>
<section>
<div class="headlines info-header"><img src="<?php echo get_template_directory_uri(); ?>/assets/images/arrow.png" alt="arrow"><?php echo $title; ?></div>
<div class="page-content"><?php echo $content; ?></div>
</section>
<?php
}
?>

wordpress shortcode render content in dashboard also

I am using a simple wordpress shortcode
function my_recent_post()
{
echo 'hello';
}
add_shortcode( 'recent', 'my_recent_post' );
with the shortcode [recent] and its working fine and visible in front page,
but the problem is, its printing the hello in the dashboard also.
below is the screenshot, can anyone please help.
Update:
I was actually trying to show posts, so can you help me with this, because it renders the lists of posts in the dashboard itself like the "hello". I tried:
function lorem_function() {
global $post;
$args = array( 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' );
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post ); ?>
<div>
<?php the_date(); ?> <br /> <?php the_title(); ?> <?php the_excerpt(); ?>
</div>
<?php endforeach;
wp_reset_postdata();
return;
}
add_shortcode('lorem', 'lorem_function');
Based on your comments to me & Nikita Dudarev, what you need to do is create a variable to hold all the post information and then return it. Using the function you posted as an example:
function lorem_function() {
global $post;
$args = array( 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' );
$postslist = get_posts( $args );
// create a variable to hold the post information
$html ="";
foreach ( $postslist as $post ) :
setup_postdata( $post );
$backgroundstyle = "";
// get the featured image and set it as the background
if ( has_post_thumbnail() ) { // make sure the post has a featured image
$imageurl = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium' ); // you can change "medium" to "thumbnail or full depending on the size you need
// add the css for the background image. You can include background-size etc ad required
$backgroundstyle = "background-image: url('".$imageurl[0]."');";
}
// add the information to the variable
$html .= '<div style="'.$backgroundstyle.'">';
$html .= get_the_date();
$html .= "<br />";
$html .= get_the_title();
$html .= get_the_excerpt();
$html .= "</div>";
endforeach;
wp_reset_postdata();
return $html;
}
add_shortcode('lorem', 'lorem_function');
Note that the_date(), the_title() and the_excerpt() all display the information (just like echo).
Instead you must use get_the_date(), get_the_title() and get_the_excerpt() - these get the same information, but instead of displaying it directly, they return it as a variable which you can then store in your html string to be returned.
Update:
As you don't want to use the variable name on each line for whatever reason, you can do it like this:
$html .= "<div>".get_the_date()."<br />".get_the_title().get_the_excerpt()."</div>";
I'm not sure why you specifically want to change it to do that - it makes absolutely no difference to how it works, it just makes it harder to read and identify any errors :-)
Your function must return a value, not output
function my_recent_post()
{
return 'hello';
}
add_shortcode( 'recent', 'my_recent_post' );

Displaying post titles in a specific category in a shortcode (wordPress)

Trying to write my first short-code that displays all post-titles in a specific Category. But it is not displaying the actual results, just the short code.
Here is what I have so far in my child theme's functions.php file:
function posts_in_cat() {
echo '<ul>';
query_posts('cat=3'); while (have_posts()) : the_post();
echo ('<li>' . the_title() . '</li>');
endwhile;
wp_reset_query();
echo '</ul>';
}
add_shortcode( 'display_posts_in_cat', 'posts_in_cat' );
And then I am calling the short code, like so [display_posts_in_cat].
Any help would be greatly appreciated as I try and learn this.
EDIT: I have gotten it to display but the link itself is being echo-ed in front of the title in text. Also, it is not displaying more than 10 of the titles and I want it to display them all. Any ideas...?? Thanks.
First and foremost, avoid using query_posts() – it's inefficient. Check out this SO answer for the skinny.
Additionally, shortcodes shouldn't make use of echo statements. Shortcodes only return text. Put simply, WordPress has PHP internally that says "when this specific shortcode is entered into the editor, replace it with the text returned from this function." Using echo causes you to immediately print those statements to the screen, rather than returning to WordPress so that it can continue with its regular process. More on the WP Codex.
And, finally, shortcode functions require $atts as a parameter.
So, with all that said, here's how I would rewrite your function:
<?php
function posts_in_cat( $atts ) {
$atts = shortcode_atts( array(
'cat' => '',
), $atts );
if ( empty( $atts['cat'] ) ) {
// If category provided, exit early
return;
}
$args = array(
'category' => $atts['cat'],
// Disable pagination
'posts_per_page' => -1
);
$posts_list = get_posts( $args );
if ( empty( $posts_list) ) {
// If no posts, exit early
return;
}
$opening_tag = '<ul>';
$closing_tag = '</ul>';
$post_content = '';
foreach ( $posts_list as $post_cat ) {
$post_content .= '<li>' . esc_html( get_the_title( $post_cat->ID ) ) . '</li>';
}
return $opening_tag . $post_content . $closing_tag;
}
add_shortcode( 'display_posts_in_cat', 'posts_in_cat' );
I'm just adding all of the content you were echoing into a handful of variables, and then returning them, concatenated, at the end. Also, I added in an if statement to exit early if there aren't any posts in the category. That way you don't have an empty <ul> element cluttering up the page.
I've also escaped the outputs, which you can read about on the Codex.
Please try this:
function posts_in_cat() { ?>
<ul class="posts">
<?php query_posts('cat=3&showposts=50'); while (have_posts()) : the_post();
?>
<li><a href='<?php the_permalink() ?>'><?php the_title(); ?></a></li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
<?php
}
add_shortcode( 'display_posts_in_cat', 'posts_in_cat' );

The_title() Wordpress using in php script

I'm trying to get the_title(); from a wordpress page to use it in a php script
my code:
<?php
global $post;
$args = array( 'numberposts' => 10, 'category_name' => 'bin-o' );
$posts = get_posts( $args );
foreach( $posts as $post ): setup_postdata($post); ?>
$project_name = the_title();
$post_id = get_page_id('$project_name');
var_dump($project_name);
?>
<a href="<?php echo get_site_url() . '/?p=' . $post_id ?>"><h1><?php the_title() ?></h1>
<?php the_content() ?></a>
The functions.php:
<?php
// Get the id of a page by its name
function get_page_id($page_name){
global $wpdb;
$page_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
return $page_name;
}
?>
The problem is it only gives the_title() When it is printed.
so I can't use the_title() to use it for a php script cause the_title will return NULL
how can I fix this so I can use the requested title to use this further in the php script
You use get_the_title().
Many wordpress in-loop functions have corresponding get_ versions, which will return the value, instead of echoing it.
I've used this now:
<?php
$project_name = trim(ucfirst(get_the_title())); //The title of current post!
$project_info = get_page_by_title($project_name);
$project_id = $project_info->ID;
?>
<a href="<?php echo get_site_url() . '/?p=' . $project_id ?>"><h1><?php the_title() ?></h1>
<?php the_content() ?></a>
The project_info get's all the project info, project_id gets the ID out of the project info and uses that to redirect to the wanted page. so I dont have to use the functions anymore

Simple Wordpress Loop result completely wrong and full of breaks and <p> elements

I am trying to make a simple loop through my wordpress posts but when it comes to output the whole thing the loop puts it anywhere but not where I said, it should be. For example the loop adds breaks and but they are not in my posts. I had this problem once but dont know anymore what i have done to get over this problem.
MY LOOP:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 4
);
$loop = new WP_Query($args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
$post_id = get_the_ID();
$content = the_content();
$headline = get_post_meta($post_id, 'headline', true);
$subtitle = get_post_meta($post_id, 'untertitel', true);
$class = wp_get_post_terms( $post_id, 'post_tag', $termsargs );
$bgimage = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
echo '<section class="section zutaten '.$class[0].'" style="background-image: url('.$bgimage.')">';
echo '<article class="zutaten-beschreibung">';
echo '<header>';
echo '<h2><strong>'.$headline.'</strong></h2>';
echo '<h3>'.$subtitle.'</h3>';
echo '<div class="zutaten-beschreibung-content">';
echo $content;
echo '</div>';
echo '</header>';
echo '</article>';
echo '</section>';
endwhile;
} else {
echo __('Fehler!');
}
wp_reset_postdata();
?>
RESULT:
What is going wrong here?
The function the_content() is printing the data. you should use get_the_content() instead.
I can see what's going wrong here. The echo $content; line is printing improper HTML. I know this because you've said in your caption there are many <p> tags where they should not be; and you're not (visibly) printing any <p> tags in this block of code.
Investigate what this method is returning: $content = the_content(); That should do the trick.

Categories