Delete WordPress post from front end - php

I have duilt a custom post type and added it to a tab on the profile page. I want to add a button to delete the post if the post author is logged in. I tried several methods but couldn't find a way to delete it since I am not trying to delete it from the single post page. Can anyone direct me on how to achieve this?
//function to print publish button
function show_publish_button(){
Global $post;
//only print fi admin
echo '<form id="myForm" name="front_end_publish" method="POST" action="">
<input type="hidden" name="pid" id="pid" value="'.$post->ID.'">
<button type="submit" name="submit" id="submit" value="delete" class="btn" style="margin-left:2px;background:#f5f5f5;"><i class="fa fa-fw fa-times"></i>delete</button>
</form>';}
// Adding Manage Event TAB content
function manage_events_content() {
$type = 'events';
$args = array (
'post_type' => $type,
'name' => $title,
'author' => bp_displayed_user_id(),
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 10,
'ignore_sticky_posts'=> 1,
);
$temp = $wp_query; // assign ordinal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
echo '<div style="background-color:#fff; padding: 20px; border-radius: 10px; margin: 20px">
<div>
<div>
<h4 style="font-weight: 400; font-size: 15px; text-transform: capitalize">Event Name:
<a style="color:#5a5a5a;" href="' . get_the_permalink() . '">' . get_the_title() . '</a>
</h4></div>
<div>
<h4>
Number of tickets available: <span>' . get_post_meta(get_the_ID(), "number-of-ticket-available", true) . '</span>
</h4>
<h4>
Number of tickets available: <span>' . get_post_meta(get_the_ID(), "number-of-confirmed-attendees", true) . '</span>
</h4>
</div>
<div>
<h4>
Start Date: <span>' . get_post_meta(get_the_ID(), "event-anfang", true) . '</span>
</h4>
<h4>
End Date: <span>' . get_post_meta(get_the_ID(), "event-ende", true) . '</span>
</h4>
</div>' . show_publish_button() . '
</div>
</div>';
endwhile;
else :
echo '<h4>You dont have any events</h4>';
endif;
$wp_query = $temp;
}

Related

How to fix Warning: Use of undefined constant active - assumed 'active' (this will throw an Error in a future version of PHP)

I'm currently working on a WirdPress Theme project using Bootstrap v5.0.2 and PHP 7.3.28.
I'm tryng to insert the Bootstrap Carousel Component in a custom widget but when I debug the code I recive this "Warning: Use of undefined constant active - assumed 'active' (this will throw an Error in a future version of PHP) in ... on line 70"
This is the line that give me the error:
echo ' class="';if ( $the_query->current_post == 0 ) : active ;endif; '"> ';
I try to change active in $active or 'active' but it doen't works.
Because I'm a newbie in PHP I don't understand what I'm doing wrong.
I hope someone can help me to solve this problem.
Thanks
This is the whole code:
<?php
// Register and load the widget
function tabulas_slider_widget() {
register_widget( 'Tabulas_Slider_Widget' );
}
add_action( 'widgets_init', 'tabulas_slider_widget' );
// Creating the widget
class Tabulas_Slider_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
public function __construct() {
parent::__construct(
// Base ID of widget
'Tabulas_Slider_Widget',
// Widget name will appear in UI
esc_html__('Last Post Slider', 'tabulas'),
// Widget description
array( 'description' => esc_html__( 'Display the 3 last post published with featured image in the form of a Slider', 'tabulas'), )
);
}
/**
* Front-end display of widget.
*
* #see WP_Widget::widget()
*
* #param array $args Widget arguments.
* #param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
// WP_Query arguments
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
);
// The Query
$the_query = new WP_Query ( $args );
echo ' <div id="carouselExampleCaptions" ';
echo ' class="carousel slide" ';
echo ' data-bs-ride="carousel" ';
echo ' data-bs-interval="10000"> ';
echo ' <div class="carousel-indicators"> ';
if ( $the_query->have_posts() ) :
echo $args['before_widget'];
if ( $title ){
echo $args['before_title'] . $title . $args['after_title'];
}
echo ' <ol class="carousel-indicators"> ';
while ( $the_query->have_posts() ) : $the_query->the_post();
echo ' <li data-target="#ExampleCarouselID" ';
echo ' data-slide-to="';$the_query->current_post;'" ';
echo ' class="';if ( $the_query->current_post == 0 ) : active ;endif; '"> ';
echo ' </li> ';
endwhile;
echo ' </ol> ';
rewind_posts();
echo ' <button type="button" ';
echo ' data-bs-target="#carouselExampleCaptions" ';
echo ' data-bs-slide-to="0" ';
echo ' class="active" ';
echo ' aria-current="true" ';
echo ' aria-label="Slide 1">';
echo ' </button> ';
echo '<button type="button" ';
echo ' data-bs-target="#carouselExampleCaptions" ';
echo ' data-bs-slide-to="1" ';
echo ' aria-label="Slide 2">';
echo ' </button> ';
echo '<button type="button" ';
echo ' data-bs-target="#carouselExampleCaptions" ';
echo ' data-bs-slide-to="2" ';
echo ' aria-label="Slide 3">';
echo '</button>';
echo '</div>';
echo ' <div class="carousel-inner"> ';
while ( $the_query->have_posts() ) : $the_query->the_post();
$thumbnail_id = get_post_thumbnail_id();
$thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'full', true );
$thumbnail_meta = get_post_meta( $thumbnail_id, '_wp_attatchment_image_alt', true );
echo ' <div class="carousel-item ';
if ( $the_query->current_post == 0 ) : active ;endif; '"> ';
if ( has_post_thumbnail() ) {
echo '<a href="';the_permalink(); echo '">';
the_post_thumbnail('full');
echo '</a>';
}
echo '<div class="carousel-caption d-none d-md-block">';
echo '<h5>';
the_title();
echo '</h5>';
echo '</div>';
echo '</div>';
endwhile;
wp_reset_query();
echo '</div>';
echo '<button class="carousel-control-prev" type="button" data-bs-target="#carouselExampleCaptions" data-bs-slide="prev">';
echo '<span class="carousel-control-prev-icon" aria-hidden="true">';
echo '</span>';
echo' <span class="visually-hidden">';esc_html_e( 'Previous', 'tabulas' );
echo '</span>';
echo '</button>';
echo '<button class="carousel-control-next" type="button" data-bs-target="#carouselExampleCaptions" data-bs-slide="next">';
echo '<span class="carousel-control-next-icon" aria-hidden="true">';
echo '</span>';
echo '<span class="visually-hidden">';esc_html_e( 'Next', 'tabulas' );
echo '</span>';
echo '</button>';
echo '</div>';
echo $args['after_widget'];
wp_reset_postdata();
endif;
}
You must echo 'active' and end of line:
echo ' class="';if ( $the_query->current_post == 0 ) : echo 'active' ;endif; echo '"> ';

Author name on main page not showing up

On my main page, I have posts that underneath have the author name. Or well, should. However nothing actually appears and I'm not sure why they aren't showing. On my sidebar.php they do show up. I've tried to see if there was actually something appearing and not being hidden by CSS, however nothing appears at ALL from the get_the_author() on the front-page
Heres my front page code:
$result = wp_get_recent_posts(array(
'numberposts' => 1,
'category' => '',
'post_status' => 'publish',
));
foreach( $result as $p ){
?>
<!-- The card itself-->
<div class="card cardcustom">
<!-- The image -->
<img class="card-img tinted" src="<?php echo get_the_post_thumbnail_url($p['ID'], array(1438, 500)); ?>" style="height: 500px; object-fit: cover; border-radius: 0px;"/>
<!-- Text over the image -->
<div class="card-img-overlay card-content">
<p href="#" class="card-text the-badge badge badge-primary"><?php
foreach(get_the_category($p['ID']) as $category) {
echo $category->name . ' ';
}
?></p><br>
<p class="posttitle card-text" style="font-weight: 600; font-size: 16px;">
<?php echo $p['post_title']?></p><br />
<?php
$authorname = get_the_author();
echo '<p class="authortext card-text">From ' . '<strong class="colorauthor">' . $authorname . '</strong>' . '</p>';
?>
</div>
</div>
<?php
}
?>
And here is the code on my sidebar:
$result = wp_get_recent_posts(array(
'numberposts' => 8,
'category' => '',
'post_status' => 'publish',
));
foreach( $result as $p ){
?>
<!-- The card itself-->
<div class="card cardcustom">
<!-- The image -->
<img class="card-img tinted" src="<?php echo get_the_post_thumbnail_url($p['ID'], array(440, 240)); ?>" style="border-radius: 0px;"/>
<!-- Text over the image -->
<div class="card-img-overlay card-content">
<p href="#" class="card-text the-badge badge badge-primary" style="border-radius: 0px;"><?php
foreach(get_the_category($p['ID']) as $category) {
echo $category->name . ' ';
}
?></p><br>
<p class="posttitle card-text" style="font-weight: 600; font-size: 16px;">
<?php echo $p['post_title']?></p><br />
<?php
$authorname = get_the_author();
echo '<p class="authortext card-text">From ' . '<strong class="colorauthor">' . $authorname . '</strong>' . '</p>';
?>
</div>
</div>
<?php
}
?>
They're almost identical but for whatever reason it refuses to show up on the main page. You can see what I mean from the screenshots below:
MAIN PAGE:
https://gyazo.com/947f49090fd90a4068725a3968e1e205
SIDEBAR ON A DIFFERENT PAGE:
https://gyazo.com/b9325be19f388dc2d64ae2fe502e3ba4
get_the_author() uses the global $authordata.
And wp_get_recent_posts() doesn't alter the main query (because it uses get_posts() under the hood).
I guess on that sidebar it loads the author of the current page. To verify create another page with another user and try to see if the author changes.
I would suggest you get the author name by get_the_author_meta('display_name', $p->post_author); (replace get_the_author_meta with the_author_meta for simply displaying it).

How to reverse array data?

I want array 1,3,5,7,9 layout will be reverse from array 0,2,4,6,8,10
for example 1st row layout- image on left and description on righ, the 2nd row layout- image on right and description on left, and continuously.
This is my function.php code
<?php
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
$image = get_field('featured_image', $term);
?>
<div class="row">
<div id="product-cat <?php echo $term->slug ?>">
<div class="two-col product-cat-image">
<img src="<?php echo $image ?>">
</div>
<div class="two-col product-cat-details">
<?php
echo '<h4>'. $term->name .'</h4>';
echo '<p>'. $term->description .'</p>';
echo '<a class="product-cat-button" href="' . get_term_link( $child, $taxonomy_name ) . '">See Products</a>';
?>
</div>
</div>
</div><?php
} ?>
CSS code:
.row{
display: flex;
margin-left: -10px;
margin-right: -10px;
margin-bottom: 15px;
}
.row .col {
flex: 1;
padding-right: 10px;
padding-left: 10px;
}
And the result still like this
my expectation will be like this:
We just need to change code in function.php as below.
<?php
$i=0;
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
$image = get_field('featured_image', $term);
if($i % 2){ ?>
<div class="row">
<div id="product-cat <?php echo $term->slug ?>">
<div class="two-col product-cat-image">
<img src="<?php echo $image ?>">
</div>
<div class="two-col product-cat-details">
<?php
echo '<h4>'. $term->name .'</h4>';
echo '<p>'. $term->description .'</p>';
echo '<a class="product-cat-button" href="' . get_term_link( $child, $taxonomy_name ) . '">See Products</a>';
?>
</div>
</div>
</div>
<?php } else {?>
<div class="row">
<div id="product-cat <?php echo $term->slug ?>">
<div class="two-col product-cat-details">
<?php
echo '<h4>'. $term->name .'</h4>';
echo '<p>'. $term->description .'</p>';
echo '<a class="product-cat-button" href="' . get_term_link( $child, $taxonomy_name ) . '">See Products</a>';
?>
</div>
<div class="two-col product-cat-image">
<img src="<?php echo $image ?>">
</div>
</div>
</div>
<?php }
$i++;
}
?>
Hope now you got all things and let me know still any help need.
I assume you want the array_diff of [0,2,4,6,8,10].
Array_diff returns what is missing in the array, if we use an range() to create the reference array then it would look like:
$yourarray = [0,2,4,6,8,10];
$diff = array_diff(range(min($yourarray), max($yourarray)), $yourarray);
//[1,3,5,7,9]
https://3v4l.org/6QY3r
you need to add following css to achieve your desired output:
when odd .product-cat div found apply order: 1; to image container(.product-cat-image) and order: 2; to detail container(.product-cat-details)
when even .product-cat div found apply order: 2; to image container(.product-cat-image) and order: 1; to detail container(.product-cat-details)
.product-cat:nth-child(odd) .product-cat-image{
-webkit-order: 1;
order: 1;
}
.product-cat:nth-child(odd) .product-cat-details{
-webkit-order: 2;
order: 2;
}
.product-cat:nth-child(even) .product-cat-image{
-webkit-order: 2;
order: 2;
}
.product-cat:nth-child(even) .product-cat-details{
-webkit-order: 1;
order: 1;
}

Delay content loading on wordpress loop to speed up page

i'm developing a real estate page, you can check it out here. As you can see, the page take some time to load, because it loads all the 50 apartments/houses at once.
To speed up the page, i want to know if there is any way to delay the load, so the page load faster.
Here is the loop code, i'm wondering what could work, for example if i put some "if/else" condition before the wordpress make a loop (while ( $newsLoop->have_posts() ) : $newsLoop->the_post();?>), but i don't know how to do it. The ideal for me is that the contents are loaded 3 on 3, or "row by row", once a row have 3 apartments.
<div class="portfolio-items2">
<?php
$newsArgs = array(
'post_type' => 'property',
"orderby" => 'meta_value_num',
"meta_key" => 'numerooff',
"order" => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'property-status',
'field' => 'slug',
'terms' => 'oneoff',
)
)
);
$newsLoop = new WP_Query( $newsArgs );
while ( $newsLoop->have_posts() ) : $newsLoop->the_post();?>
<div class="col-md-4 shortcode-col listing_wrapper <?php meta('seletoroff');?>" >
<div class="property_listing" data-link="http://www.onecia.com.br/imoveis-de-luxo/<?php /* Property ID if exists */ $property_id = get_post_meta($post->ID, 'REAL_HOMES_property_id', true); if(!empty($property_id)){ echo ''.$property_id; }?>">
<div class="listing-unit-img-wrapper">
<div class="property_media"> </div>
<a href="http://www.onecia.com.br/imoveis-de-luxo/<?php /* Property ID if exists */ $property_id = get_post_meta($post->ID, 'REAL_HOMES_property_id', true); if(!empty($property_id)){ echo ''.$property_id; }?>">
<img width="525" height="350" src="<?php meta('imagemoff');?>" class="lazyload img-responsive wp-post-image" alt="" sizes="(max-width: 525px) 100vw, 525px" />
</a>
<div class="tag-wrapper"><div class="featured_div"><?php meta('porcentooff');?></div></div>
</div>
<h4>
<a href="http://www.onecia.com.br/imoveis-de-luxo/<?php /* Property ID if exists */ $property_id = get_post_meta($post->ID,'REAL_HOMES_property_id', true); if(!empty($property_id)){ echo ''.$property_id; }?>">
<?php meta('titulooff');?>
</a>
</h4>
<div class="property_location_image">
<a href="http://www.onecia.com.br/imoveis-de-luxo/<?php /* Property ID if exists */ $property_id = get_post_meta($post->ID,'REAL_HOMES_property_id', true); if(!empty($property_id)){ echo ''.$property_id; }?>" rel="tag">
<span style="font-size: 15px;"><?php meta('bairrooff');?> </span>
</a>
</div>
<br>
<div class="property_listing_details">
<?php
$post_meta_data = get_post_custom($post->ID);
if( !empty($post_meta_data['REAL_HOMES_property_size'][0]) ) {
$prop_size = $post_meta_data['REAL_HOMES_property_size'][0];
echo '<div class="inforoom">'. $prop_size .'m² <div class="info_labels"><img src="http://www.onecia.com.br/wp-content/themes/site2016/images/icon-size.png" style="max-width: 14%; height: auto;"></div></div>';
}
if( !empty($post_meta_data['REAL_HOMES_property_bedrooms'][0]) ) {
$prop_bedrooms = floatval($post_meta_data['REAL_HOMES_property_bedrooms'][0]);
$bedrooms_label = ($prop_bedrooms > 1)? __('Bedrooms','framework' ): __('Bedroom','framework');
echo '<div class="infobath">'. $prop_bedrooms .'<div class="info_labels"><img src="http://www.onecia.com.br/wp-content/themes/site2016/images/icon-bed.png" style="max-width: 17%; height: auto;"></div></div>';
}
if( !empty($post_meta_data['REAL_HOMES_property_bathrooms'][0]) ) {
$prop_bathrooms = floatval($post_meta_data['REAL_HOMES_property_bathrooms'][0]);
$bathrooms_label = ($prop_bathrooms > 1)?__('Bathrooms','framework' ): __('Bathroom','framework');
echo '<div class="infosize">'. $prop_bathrooms .'<div class="info_labels"><img src="http://www.onecia.com.br/wp-content/themes/site2016/images/icon-bath.png" style="max-width: 17%; height: auto;">
</div></div>';
}
?>
</div>
<div class="listing_unit_price_wrapper">
<span class="price_label price_label_before" style="text-decoration: line-through;">De: R$ <?php meta('valordeoff');?></span><br>
<span style="text-decoration: underline; font-size: 22px; font-weight: bold;">Por: R$ <?php meta('valoroff');?> </span>
<span class="price_label"></span>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
I've already installed some plugins, but none of them worked.

Variable appearing above html in php when using return

I am trying to put the variable $about inside of the html I am returning...however when I view the output, it puts $about above the html code...
function cc_shortcode( $atts ) {
$other_page = 498;
$about = the_field('cc_about_this_course', $other_page);
extract( shortcode_atts( array(
'id' => '',
), $atts) );
$result = '<div class="main">
<div class="accordion">
<div class="accordion-section">
<a class="accordion-section-title active" href="#accordion-1">ABOUT THIS COURSE<i class="fa fa-arrow-down"></i></a>
<div id="accordion-1" class="accordion-section-content open" style="display: block;">
<p>' . $about . '</p>
</div>
</div>
';
return $result;
}
https://www.advancedcustomfields.com/resources/the_field/
the_field used for displaying value from the current post that's why it display value..
solution to use get_field ; https://www.advancedcustomfields.com/resources/get_field/
if the there is not get_field.. (for common problems)... like this..
you can ob_start(); and ob_get_clean for you problem
function cc_shortcode( $atts ) {
$other_page = 498;
ob_start();
the_field('cc_about_this_course', $other_page);
$about = ob_get_clean();
extract( shortcode_atts( array(
'id' => '',
), $atts) );
$result = '<div class="main">
<div class="accordion">
<div class="accordion-section">
<a class="accordion-section-title active" href="#accordion-1">ABOUT THIS COURSE<i class="fa fa-arrow-down"></i></a>
<div id="accordion-1" class="accordion-section-content open" style="display: block;">
<p>' . $about . '</p>
</div>
</div>
';
return $result;
}

Categories